简体   繁体   English

将std :: initializer_list传递为非类型模板参数

[英]Passing std::initializer_list as a non-type template argument

I have a problem with the following code: 我的以下代码有问题:

#include <deque>
#include <initializer_list>
#include <string>

struct EnumItem
{
    inline operator int() const {
        return id;
    }

    std::string name;
    int id;
};

template <const std::initializer_list<EnumItem>& items>
class Enum
{
public:


private:
    static const std::deque<EnumItem> _items;
};

template <const std::initializer_list<EnumItem>& items>
const std::deque<EnumItem> Enum<items>::_items{ items };

int main() 
{
   Enum<{{"0", 0}}> test;
   return 0;
}

It doesn't compile, throws numerous syntax errors about my test instantiation: 它不编译,引发关于我的test实例的许多语法错误:

2>error C2059: syntax error : '{'    
2>error C2143: syntax error : missing ';' before '{'     
2>error C2143: syntax error : missing '>' before ';'     
2>error C2976: 'Enum' : too few template arguments     
2>: see declaration of 'Enum'       
2>error C2447: '{' : missing function header (old-style formal list?)      
2>error C2059: syntax error : '>'    

What am I doing wrong and how to do it right? 我做错了什么,怎么做对了?

initializer_list is not a type which can be used with non-type template parameters. initializer_list不是可与非类型模板参数一起使用的类型。 In C++14, these are restricted to enums, integers, pointers of all kinds, lvalue references, and nullptr_t . 在C ++ 14中,这些限制为枚举,整数,各种指针,左值引用和nullptr_t

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM