using std::initializer_list and int as function parameter causes compiler "wrong" translation

StackOverflow https://stackoverflow.com/questions/22139674

  •  19-10-2022
  •  | 
  •  

Question

Here is the code, I find a strange thing about std::initializer_list . I use vs2013. Thanks for help.

struct TempStruct
{
  int t1;
  int t2;
};

int Test_A(int a, int b)
{
    std::cout << " int a, int b" << '\n';

    return 1;
}

TempStruct Test_A(std::initializer_list<int> a, std::initializer_list<int> b)
{
    std::cout << "initializer_list" << '\n';

    TempStruct Temp;

    Temp.t1 = 1;

    Temp.t2 = 2;

    return Temp;
}

int main()
{
    auto a_test = Test_A(1, 1);

    auto b_test = Test_A({ 1 }, {});

    return 1;
}

Result(std::cout) I want:

int a, int b
initializer_list

Result(std::cout) from vs2013

int a, int b
int a, int b

In the vs2013 IDE, if I move mouse on "b_test", it shows b_test is TempStruct. After build, the result isn't. Is this a bug of vs2013 or miss-use of initializer_list ?

No correct solution

OTHER TIPS

This statement:

Test_A({ 1 }, {});

Can be interpreted as 2 initializer lists containing 1 and containing nothing, but it can also be interpreted as:

Test_A(int{ 1 }, int{});

This is the so-called uniform initialization syntax. The Intellisense has its own "compiler" implementation and often shows errors where there are none or shows wrong types. This happens a lot with the new C++11 features.

You can try using double braces to disambiguate:

Test_A({{ 1 }}, {{}});

I can't verify this at the moment though, Visual Studio and iPad don't mix very well ;)

You should also consider accepting only initializer lists and handle the special case when only 1 item is passed, as most callers will probably be confused what the difference between these 2 overloads is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top