I'm trying to use an initialization-list to pass a list of keywords to a tokenizer to register. But it does not work in Visual Studio 2013. It works in gcc at ideone.com. Is there any way to use this or a similar syntax in VS?
struct Keyword
{
const char* name;
int id;
};
void AddKeywords(int group, std::initializer_list<Keyword>& lis) {}
// usage
AddKeywords(ITEM_TYPE, {
{ "weapon", IT_WEAPON },
{ "armor", IT_ARMOR }
});
Full error:
item.cpp(810): error C2664: 'void AddKeywords(int,std::initializer_list<Keyword> &)' : cannot convert argument 2 from 'initializer-list' to 'std::initializer_list<Keyword> &'
You are trying to bind a temporary to a non-const reference;
std::initializer_list<Keyword>& lis
Try either;
std::initializer_list<Keyword> const& lis
Or
std::initializer_list<Keyword> lis
When building with GCC, enable -Wall -pedantic
it should give you an error then as well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With