相关疑难解决方法(0)

编译时模板`std :: integral_constant`计数器 - 如何实现它?

我有几种类型,我想std::integral_constant在编译时将一个顺序ID值"绑定" 到每个类型.

例:

struct Type00 { };
struct Type01 { };
struct Type02 { };
struct Type03 { };
struct TypeXX { };
struct TypeYY { };

template<typename T> struct TypeInfo
{
    using Id = std::integral_constant<int, ???>;
};

int main()
{
     cout << TypeInfo<Type00>::Id::value; // Should always print 0
     cout << TypeInfo<Type01>::Id::value; // Should always print 1
     cout << TypeInfo<Type02>::Id::value; // Should always print 2
     cout << TypeInfo<Type03>::Id::value; // Should always print 3
     cout << TypeInfo<TypeXX>::Id::value; // Should …
Run Code Online (Sandbox Code Playgroud)

c++ templates constexpr c++11 c++14

6
推荐指数
1
解决办法
927
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1

constexpr ×1

templates ×1