10

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 ?

Magix
  • 4,989
  • 7
  • 26
  • 50
  • Are you getting this error just from the single line of code you show? Is the error message the full and complete output you get? In the answer to any of those questions is "no" then please [edit] your question to include both the [mcve] needed to reproduce the error, and also include a full and complete copy-paste of the full output. – Some programmer dude Jul 06 '20 at 07:54
  • yes. The full output log is in the link and this line is sufficient to reproduce. – Magix Jul 06 '20 at 07:56
  • 1
    @Magix can you show the line in your code in what function or constructor of vector you are using ? or just point out which call in vector is giving the error ? – darune Jul 06 '20 at 08:23
  • This corresponds to no call, and the error points to the beginning of the definition of the class that holds the member. Will update the question to reflect that – Magix Jul 06 '20 at 08:24
  • 1
    @Magix also add compiler (or stl) version – darune Jul 06 '20 at 08:26
  • 1
    @Magix it just "sounds like" in the error it is constructing from a range input - although i can't be sure. Are you sure you don't have anything using or operating or constructing on the vector ? I also see the number 1000000 a lot in the linked error message. It could be related or not. Again, try to figure out of which line of code is generating the error if possible. – darune Jul 06 '20 at 08:32
  • I found the culprit, it was an unintended copy in an unrelated lambda which wasn't traced by the compiler. I was lucky to find it ! Thanks for the help. – Magix Jul 06 '20 at 08:52
  • 5
    A proper [mcve] might be helpful. – Some programmer dude Jul 06 '20 at 08:53
  • Producing minimal reproducible examples is often more work than sorting out an issue... so then what's the point of stackoverflow? – mnr Mar 05 '21 at 13:43
  • @mnr Doing said work might mean the user figures out and resolves their own problem and then doesn't have to post it, leaving a question that has no answer except a guess... so then what's the problem with that? – underscore_d Jul 06 '21 at 17:35
  • 1
    Firstly, it is then not helpful to other users. Secondly, a guess from someone experienced can be extremely valuable. For instance when trying to produce a minimal example, it may give you a very good clue where to start looking and what part of your code you should cut out. That guess may also help other people in similar situations. – mnr Aug 14 '21 at 14:03

2 Answers2

16

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);
}
darune
  • 10,480
  • 2
  • 24
  • 62
-2

to reproduce the error result type must be constructible from value type of input range

godbolt

#include <vector>
#include <string>
#include <iostream>

int main() {

typedef std::vector<std::string> StrVec;

StrVec s1("a", "b"); // error
// stl_uninitialized.h:127:7: error: static_assert failed due to requirement 'is_constructible<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, const char &>::value' "result type must be constructible from value type of input range"

//StrVec s2("a", "b", "c"); // error
// error: no matching constructor for initialization of 'StrVec' (aka 'vector<basic_string<char> >')

StrVec s3({"a", "b"}); // ok
StrVec s4({"a", "b", "c"}); // ok

typedef std::vector<int> IntVec;

IntVec i1(1, 2); // silent error
for (auto i : i1) {
    std::cout << "i = " << i << '\n';
}
// silent error:
// output:
// i = 2
// expected:
// i = 1
// i = 2

//IntVec i2(1, 2, 3); // error
// error: no matching constructor for initialization of 'IntVec' (aka 'vector<int>')

IntVec i3({1, 2}); // ok
IntVec i4({1, 2, 3}); // ok

}
milahu
  • 2,447
  • 1
  • 18
  • 25