让模板采用类型或值

Fis*_*ans 3 c++ templates types

我想让MemoryPool在运行时动态分配和取消分配内存,而无需让OS尝试加快代码执行(和学习)的速度。为了简化语法,我希望能够指定内存段的大小,作为它们包含的类型或原始大小。

为此,我想制作一个可以使用Type或size_t并继续发送sizeof Type或size的模板。

template<size_t SegmentSize>
class MemoryPool_Internal
{
public:
    static const size_t Size = SegmentSize;
    /*Using SegmentSize to do logic*/
};

template<size_t Size>
class MemoryPool : public MemoryPool_Internal<Size> { };

template<class Size>
class MemoryPool : public MemoryPool_Internal<sizeof(Size)> { };
Run Code Online (Sandbox Code Playgroud)

我希望上面的代码片段发生的事情是

std::cout << MemoryPool<5>::Size << std::endl;
std::cout << MemoryPool<int>::Size << std::endl;
Run Code Online (Sandbox Code Playgroud)

打印5和sizeof(int)。

但是5会引发C3855,因为它不是一个类;而int会引发E0254,因为第一个模板中不允许类型。有什么办法可以在编译时解决每个目标模板的问题?

bol*_*lov 8

您不能完全那样做。该语言不允许这种语法。但是,您只能使用类型模板并创建一个类型来容纳显式大小:

template <std::size_t SegmentSize>
struct ExplicitSize
{
    static constexpr auto Size = SegmentSize;
};

template <class T>
constexpr std::size_t SegmentSize = sizeof(T);
template <std::size_t Size>
constexpr std::size_t SegmentSize<ExplicitSize<Size>> = Size;


template<class SizeSpecifier>
class MemoryPool_Internal
{
public:
    static const size_t Size = SegmentSize<SizeSpecifier>;
    /*Using Size to do logic*/
};

static_assert(MemoryPool_Internal<ExplicitSize<32>>::Size == 32);
static_assert(MemoryPool_Internal<int>::Size == sizeof(int));
Run Code Online (Sandbox Code Playgroud)

或者,仅使用值模板并使用sizeof

MemoryPool_Internal<32>
MemoryPool_Internal<sizeof(int)>
Run Code Online (Sandbox Code Playgroud)