[英]Passing std::initializer_list as a non-type template argument
我的以下代碼有問題:
#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;
}
它不編譯,引發關於我的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 : '>'
我做錯了什么,怎么做對了?
initializer_list
不是可與非類型模板參數一起使用的類型。 在C ++ 14中,這些限制為枚舉,整數,各種指針,左值引用和nullptr_t
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.