Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert from 'initializer-list' to 'std::initializer_list<Keyword> &'

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> &'
like image 707
Tomashu Avatar asked Feb 09 '23 11:02

Tomashu


1 Answers

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.

like image 105
Niall Avatar answered Feb 12 '23 00:02

Niall