4

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> &'
Niall
  • 30,036
  • 10
  • 99
  • 142
Tomashu
  • 515
  • 1
  • 11
  • 20
  • That shouldn't compile anyway as rvalues cannot bind to non-const lvalue references. – TartanLlama Jul 24 '15 at 11:57
  • possible duplicate of [How come a non-const reference cannot bind to a temporary object?](http://stackoverflow.com/questions/1565600/how-come-a-non-const-reference-cannot-bind-to-a-temporary-object) – NathanOliver Jul 24 '15 at 12:01

1 Answers1

8

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.

Niall
  • 30,036
  • 10
  • 99
  • 142