Search code examples
c++vectorcompiler-errorsinitializationmove-semantics

"result type must be constructible from value type of input range" when creating a std::vector


I have a class member that looks like this

class Controller {
    protected:
    // other stuff
    std::vector<Task<event_t, stackDepth>> taskHandlers; 

    //some more stuf
}

The Task class is non-defaultConstructible, non-copyConstructible, non-copyAssignable but is moveConstructible and moveAssignable. Everything I could read (notably, std::vector documentation) leads me to think that this should compile, but the error list looks like this (full output here) :

/usr/include/c++/9/bits/stl_uninitialized.h:127:72: error: static assertion failed: result type must be constructible from value type of input range
  127 |       static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
      |                                                                        ^~~~~

Making Task defaultConstructible did not help. The error points to the definition of the class Controller. I am using GCC 9.3.0 in c++17 mode. Did I do anything wrong ?


Solution

  • My best guess, given the current information, is that you messed up the move constructor syntax somehow - working example using just emplace_back:

    The below compiles fine, link to godbolt:

    #include <vector>
    
    class X
    {
    public:
        X(int i) : i_(i){}
        X() = delete;
        X(const X&) = delete;
        X(X&&) = default;//change this to 'delete' will give a similar compiler error
    private:
        int i_;
    };
    
    
    int main() { 
        std::vector<X> x;
        x.emplace_back(5);
    }