user3910279 user3910279 - 9 months ago 40
C++ Question

Creating a pair from temporaries

I try to construct a pair from temporaries. From what I understand, std::pair provides the necessary constructors, but I cannot make it work. This is my minimal example:

#include <utility>

struct Test {
Test() : a(1.0) {}

double a;
Test(Test&&) = default;
Test(const Test&) = delete;
Test& operator=(Test&&) = delete;

int main (int argc, char** argv) {
std::pair<Test, double> result(Test(), 0.0);

I tried to compile this with
clang++-3.8 --std=c++14
The copy constructor for Test is invoked by pair. Because it was deleted, I get the error
call to deleted constructor of 'Test'
. It does not seem to be a problem with the compiler though, because I get a similar error with gcc, see

Can someone explain to me why the above code fails to compile?


My gcc (6.1.1) gives a slightly different error message, which is more helpful:

t.C:8:3: note: declared private here
   Test(Test&&) = default;

Your move constructor is private. It obviously must be public.