English 中文(简体)
我如何根据模板价值有条件地宣布一个或多个阵列?
原标题:How do I conditionally declare one or more arrays based on template value?

编辑时,我需要把记忆分配给三个不同的阵列,每个阵列与三个同等规模的记忆库分别分配。 从第一个银行开始,这些阵列被宣布为最高银行规模。 其余记忆由下一家银行分配。 银行不相邻。

举例来说,假设我们需要分配2.5K的记忆,每个银行的最大规模是1K。 我们在银行1中拨款1K,在2行中再分配1K,其余500英特在3行:

std::array<uint8_t, 1000> bank1; // 1K
std::array<uint8_t, 1000> bank2; // 1K
std::array<uint8_t, 500> bank3; // 500 bytes

如果在随后的银行中没有留下任何记忆,那么它就没有宣布一个阵列。 如果有的话,它只是失败了。

迄今为止,我难以提出申报和分行。

// Define the maximum bank size (1K in this example)
constexpr std::size_t MaxBankSize = 1000;

// Helper function to calculate the remainder size
template <std::size_t MemSize, std::size_t BankSize>
constexpr std::size_t CalculateRemainder() {
    return (MemSize > BankSize) ? (MemSize - BankSize) : 0;
}

// Bank1: Allocate up to MaxBankSize
template <std::size_t MemSize>
using Bank1 = std::enable_if_t<(MemSize <= MaxBankSize), std::array<uint8_t, MemSize>>;

// Bank2: Allocate from Bank1 and calculate remainder
template <std::size_t MemSize>
using Bank2 = std::conditional_t<(MemSize > MaxBankSize),
    Bank1<MaxBankSize>,
    Bank1<MemSize>>;

// Bank3: Allocate from Bank2 and calculate remainder
template <std::size_t MemSize>
using Bank3 = std::conditional_t<(MemSize > MaxBankSize * 2),
    Bank2<MaxBankSize>,
    Bank2<MemSize>>;
问题回答

答案是每个银行都有正确的规模,但并不造成一家空银行由于SFINAE而消失。 当银行空闲时,它仍被宣布,但规模为零。

所有测试均采用<代码>MaxBankSize, 编号为1000

虽然这些阵列可以称为“全球”(即档案级或名称空间)范围,但此处的测试范围是“地方”(即区块)范围。

Note: when the arrays are local, you can use N_Banks<MemSize> with constexpr-if to prevent the unwanted banks from being declared.

// main.cpp
#include <array>
#include <cstddef>
#include <iostream>

// Define the maximum bank size (1K in this example)
constexpr std::size_t MaxBankSize = 1000;

constexpr std::size_t zero{};
constexpr std::size_t one{ 1u };
constexpr std::size_t two{ 2u };
constexpr std::size_t three{ 3u };

template< std::size_t MemSize >
constexpr std::size_t N_FullBanks
    = MemSize >= MaxBankSize 
    ? MemSize / MaxBankSize 
    : zero;

template< std::size_t MemSize >
constexpr std::size_t N_PartialBanks
    = MemSize >= MaxBankSize
    ? (MemSize % MaxBankSize ? one : zero)
    : zero;

template< std::size_t MemSize >
constexpr std::size_t N_Banks
    = N_FullBanks<MemSize> + N_PartialBanks<MemSize>;

template< std::size_t MemSize >
constexpr std::size_t Size_PartialBank 
    = MemSize >= MaxBankSize
    ? MemSize - N_FullBanks<MemSize> * MaxBankSize
    : MemSize;

template< std::size_t MemSize >
constexpr std::size_t Size_Bank1 
    = N_FullBanks<MemSize> >= one 
    ? MaxBankSize 
    : Size_PartialBank<MemSize>;

template< std::size_t MemSize >
constexpr std::size_t Size_Bank2 
    = MemSize < MaxBankSize ? zero
    : N_FullBanks<MemSize> >= two ? MaxBankSize 
    : N_FullBanks<MemSize> == one ? Size_PartialBank<MemSize>
    : zero;

template< std::size_t MemSize >
constexpr std::size_t Size_Bank3 
    = MemSize < MaxBankSize ? zero
    : N_FullBanks<MemSize> >= three ? MaxBankSize 
    : N_FullBanks<MemSize> == two ? Size_PartialBank<MemSize>
    : zero;

template< std::size_t MemSize >
void test()
{
    std::array<std::uint8_t, Size_Bank1<MemSize>> bank1;
    std::array<std::uint8_t, Size_Bank2<MemSize>> bank2;
    std::array<std::uint8_t, Size_Bank3<MemSize>> bank3;

    std::cout
        << "MemSize: " << MemSize
        << "
  bank1.size(): " << bank1.size()
        << "
  bank2.size(): " << bank2.size()
        << "
  bank3.size(): " << bank3.size()
        << "

";
}
int main()
{
    test<500>();
    test<1000>();
    test<1500>();
    test<2500>();
    test<3500>();
}
// end file: main.cpp

Output:

MemSize: 500
  bank1.size(): 500
  bank2.size(): 0
  bank3.size(): 0

MemSize: 1000
  bank1.size(): 1000
  bank2.size(): 0
  bank3.size(): 0

MemSize: 1500
  bank1.size(): 1000
  bank2.size(): 500
  bank3.size(): 0

MemSize: 2500
  bank1.size(): 1000
  bank2.size(): 1000
  bank3.size(): 500

MemSize: 3500
  bank1.size(): 1000
  bank2.size(): 1000
  bank3.size(): 1000




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签