A non-const
reference parameter, such as an int&
, can only refer to an “lvalue,” which is a named variable.
auto takes_nonconst_reference = [](int&){};
auto takes_const_reference = [](const int&){};
auto takes_value = [](int){};
auto returns_int = []{return 42;};
int foo = 1;
// OK
takes_nonconst_reference(foo);
takes_const_reference(foo);
takes_const_reference(returns_int());
takes_value(foo);
takes_value(returns_int());
// compilation error, value returned from a function is not a named variable
takes_nonconst_reference(returns_int());
In this particular case, since your class is storing a copy of the constructor parameter, you should pass it by value (int
, not int&
nor const int&
).