Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;在类型列表中创建模板专业化_C++_Templates - Fatal编程技术网

C++ C++;在类型列表中创建模板专业化

C++ C++;在类型列表中创建模板专业化,c++,templates,C++,Templates,我有以下结构: template<typename tag_type> struct tag{ typedef tag_type type; enum { value = -1 } }; 模板 结构标签{ 类型定义标签类型; 枚举{value=-1} }; 我使用这个类作为运行时标识特定类的typeid。这些类中的每一个都需要在类型列表中列出,每个标记专门化都需要有不同的值 是否有办法使值等于列表中的专门化索引 我的目标是尽可能容易地维护具有唯一值的专用标记的列表

我有以下结构:

template<typename tag_type>
struct tag{
    typedef tag_type type;
    enum { value = -1 }
};
模板
结构标签{
类型定义标签类型;
枚举{value=-1}
};
我使用这个类作为运行时标识特定类的typeid。这些类中的每一个都需要在类型列表中列出,每个标记专门化都需要有不同的值

是否有办法使
等于列表中的专门化索引

我的目标是尽可能容易地维护具有唯一值的专用
标记的列表。。。(我需要确保列表中的每个类型都有唯一的值,否则系统的一部分将崩溃)


编辑:我没有提到我在编译时使用这些值…

如果您不介意这些值在运行时之前不可用,您可以利用为全局对象调用的构造函数为您注册类。也许是这样的:

template<typename tag_type>
struct tag {
    typedef tag_type type;
    int value;
    // other info can go here, like a string representation of the class name

    tag(void) {
        register_class(this);
    }
};

#define ADD_CLASS(tag_type) tag<tag_type> __g_tag_ ## tag_type

extern int __g_class_counter;

template<typename tag_type>
static inline void register_class(tag<tag_type> *ptag) {
    ptag->value = __g_class_counter++;
    // TODO: anything else
}

// in some CPP file
int __g_class_counter = 0;
模板
结构标签{
类型定义标签类型;
int值;
//其他信息可以放在这里,比如类名的字符串表示
标签(无效){
注册课程(本课程);
}
};
#定义添加类(标记类型)标记u g_标记35;#标记类型
外部内部类计数器;
模板
静态内嵌无效寄存器_类(标记*ptag){
ptag->value=uuuu类_u计数器++;
//托多:还要别的吗
}
//在一些CPP文件中
int uuu g_u类计数器=0;

然后,只要在需要将类添加到列表中时使用宏
ADD\u CLASS
(您可能需要将
\uu g\u CLASS\u计数器
移到其他位置,并对其进行
extern
声明)。

这是您想要的吗?它使用C++11功能,即可变模板。结构的
index_返回类型列表中某个类型的索引,如果类型列表不包含给定类型,则返回-1。它是从type_list类中使用的帮助器结构。type_list类获取一个类列表,并提供子类模板
标记
,该模板通过使用
类模板的
索引来访问列表中各个类型的索引

template <int, class...>
struct index_of;

template <int n, class type, class first, class ... types>
struct index_of<n, type, first, types...>
{
        static constexpr int value = index_of<n+1, type, types...>::value;
};

template <int n, class type, class ... types>
struct index_of<n, type, type, types...>
{
        static constexpr int value = n;
};

template <int n, class type>
struct index_of<n, type>
{
        static constexpr int value = -1;
};

template <class ... types>
struct type_list
{
        template <class type>
        struct tag
        {
            static constexpr int value = index_of<0, type, types...>::value;
        };
};
模板
结构索引;
模板
结构索引
{
静态constexpr int value=index_of::value;
};
模板
结构索引
{
静态constexpr int value=n;
};
模板
结构索引
{
静态constexpr int值=-1;
};
模板
结构类型列表
{
模板
结构标签
{
静态constexpr int value=index_of::value;
};
};
用法:

using my_list = type_list<int, float, double>;
std::cout << "Tag of int: " << my_list::tag<int>::value << std::endl;
使用我的列表=键入列表;

std::cout我认为您的意思的非C++11实现,尽管您没有真正指定类型列表的结构

template <typename H, typename T>
struct typelist {
    typedef H head;
    typedef T tail;
};

template <typename T, typename List, int N>
struct typelist_index_i
{
    typedef typename List::tail tail;
    enum {
        value = N + typelist_index_i<T, tail, N + 1>::value
    };
};

template <typename List, int N>
struct typelist_index_i<typename List::tail, List, N>
{
    enum {
        value = N + 1
    };
};
template <typename List, int N>
struct typelist_index_i<typename List::head, List, N>
{
    enum {
        value = N
    };
};

template <typename T, typename List>
struct typelist_index
{
    enum {
        value = typelist_index_i<T, List, 0>::value
    };
};

class A {};
class B {};
class C {};

typedef typelist<A, typelist<B, C> > the_types;

template<typename tag_type>
struct tag{
    typedef tag_type type;
    enum { value = typelist_index<tag_type, the_types>::value };
};

int main()
{
    std::cout << tag<A>::value << std::endl; // 0
    std::cout << tag<B>::value << std::endl; // 1
    std::cout << tag<C>::value << std::endl; // 2

    system("pause");
    return 0;
}
模板
结构类型表{
H型头;
T型尾;
};
模板
结构类型列表索引
{
typedef typename列表::tail tail;
枚举{
value=N+typelist\u index\u i::value
};
};
模板
结构类型列表索引
{
枚举{
值=N+1
};
};
模板
结构类型列表索引
{
枚举{
值=N
};
};
模板
结构类型列表索引
{
枚举{
value=typelist\u index\u i::value
};
};
A类{};
B类{};
C类{};
类型定义类型列出_类型;
模板
结构标签{
类型定义标签类型;
枚举{value=typelist_index::value};
};
int main()
{

std::难道我现在没有测试它,但你的解决方案似乎正是我需要的