Search code examples
c++stdstdarray

error: C2661: 'std::array<uint,3>::array': no overloaded function takes 3 arguments


I'm receiving this error:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\xmemory0:881: error: C2661: 'std::array<uint,3>::array': no overloaded function takes 3 arguments

At this line of code:

    template<class _Objty,
        class... _Types>
        static void construct(_Alloc&, _Objty * const _Ptr, _Types&&... _Args)
        {   // construct _Objty(_Types...) at _Ptr
        ::new (const_cast<void *>(static_cast<const volatile void *>(_Ptr)))
            _Objty(_STD forward<_Types>(_Args)...);          // ** Error is here
        }

I'm using std::array<uint, 3> like this:

void MyClass::myMethod(
        // ...
        , std::vector<std::array<uint, 3>> &indices
        // ...
        , const size_t ssteps
        // ...
        )
{
    // ...
    
    indices.reserve(2*ssteps);
    auto steps = int(ssteps);
    auto offs = steps;
    auto last = steps - 1;

    // ...

    indices.emplace_back(0, last, offs);
    indices.emplace_back(last, offs + last, offs);

    // ...
}

I don't understand why this error is happening inside the xmemory0 included file. Any hint is appreciated.


Solution

  • As @kakkoko already pointed out:

    indices.emplace_back(0, last, offs) calls constructor array<uint, 3>::array(0, last, offs) but array doesn't have such constructor;

    Unfortunately, an additional pair of curly braces is not sufficient:

      indices.emplace_back({ 0u, last, offs }); // Doesn't work! :-(
    

    Output:

    main.cpp: In function 'int main()':
    main.cpp:11:42: error: no matching function for call to 'std::vector<std::array<unsigned int, 3> >::emplace_back(<brace-enclosed initializer list>)'
       11 |   indices.emplace_back({ 0u, last, offs });
          |                                          ^
    In file included from /usr/local/include/c++/10.2.0/vector:72,
                     from main.cpp:2:
    /usr/local/include/c++/10.2.0/bits/vector.tcc:109:7: note: candidate: 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {}; _Tp = std::array<unsigned int, 3>; _Alloc = std::allocator<std::array<unsigned int, 3> >; std::vector<_Tp, _Alloc>::reference = std::array<unsigned int, 3>&]'
      109 |       vector<_Tp, _Alloc>::
          |       ^~~~~~~~~~~~~~~~~~~
    /usr/local/include/c++/10.2.0/bits/vector.tcc:109:7: note:   candidate expects 0 arguments, 1 provided
    

    Live Demo on coliru

    So, the elements have to be constructed explicitly:

      indices.emplace_back(std::array<uint, 3>{ 0u, last, offs }); // Does work. :-)
    

    Live Demo on coliru

    or std::vector::push_back() has to be used instead:

      indices.push_back(std::array<uint, 3>{ 0u, last, offs });
    

    Live Demo on coliru


    A complete demo:

    #include <array>
    #include <iostream>
    #include <vector>
    
    using uint = unsigned;
    
    int main()
    {
      std::vector<std::array<uint, 3>> indices;
      const uint last = 3, offs = 4;
      
      indices.emplace_back(std::array<uint, 3>{ 0u, last, offs });
      indices.push_back({ last, offs + last, offs });
      
      std::cout << "indices: {";
      const char *sep = "\n";
      for (const std::array<uint, 3> &indicesI : indices) {
        std::cout << sep << "  {";
        const char *sepI = " ";
        for (const uint i : indicesI) {
          std::cout << sepI << i;
          sepI = ", ";
        }
        std::cout << " }";
        sep = ",\n";
      }
      std::cout << "\n}\n";
    }
    

    Output:

    indices: {
      { 0, 3, 4 },
      { 3, 7, 4 }
    }
    

    Live Demo on coliru