1

I wanted to find a convenient way to check if a value was in a set of values. If I create a simple function like the following, everything works well:

bool IsIn(const int& value, initializer_list<int> initializerList)
{
    vector<int> v(initializerList);
    return find(v.begin(), v.end(), value) != v.end();
}

bool MyFunction()
{
    return IsIn(1, { 1,2,3 });
}

if I now turn this function into a more user-friendly operator >>, i get an error C2059: syntax error: '{'

bool operator >>(const int& value, initializer_list<int> initializerList)
{
    vector<int> v(initializerList);
    return find(v.begin(), v.end(), value) != v.end();
}

bool MyFunction()
{
    return 1 >> { 1, 2, 3 };
}

I did not find in C++ specification that the use of a initializer list as parameter was limited to "normal" functions. Is this a bug or am I doing something wrong? (I am using VS2017)

Leto
  • 11
  • 2
  • `Is this a bug or am I doing something wrong?` You're doing it wrong... – P0W Oct 18 '17 at 09:39
  • 3
    `{ 1, 2, 3 }` is not an `std::initializer_list`. It's some untyped thing that depending on context does various other things. `1 >> initializer_list{ 1, 2, 3 }` should work fine. – nwp Oct 18 '17 at 09:41
  • 2
    Also, this is not a very clear overload to begin with, better stick with the named function. – Baum mit Augen Oct 18 '17 at 09:42

0 Answers0