Search code examples
c++visual-studiovisual-studio-2012stdarray

How to give a default value to an std::array with Visual C++ 2012?


How to give a default value to an std::array? For example, something like

void f(std::array<int, 3> pt = std::array<int, 3>{0, 1, 2});

After checking with helpful comments. I think it is due to the compiler. How to work it around with VS 2012 without creating a function like std::array<int, 3> MakeArray(...) ?


Solution

  • Try the following

    void f(std::array<int, 3> pt = {0, 1, 2});
    

    Or I would write simply

    void f(std::array<int, 3> = {0, 1, 2});
    

    In GCC this code is not compiled. It seems it is a bug of the compiler. However you can write for GCC either as

    void f( std::array<int, 3> = std::array<int, 3>( { 1, 2, 3 } ) );
    

    or

    void f( std::array<int, 3> = { { 1, 2, 3 } } );