Invalid Initialization of Non-Const Reference with C++11 Thread

Invalid initialization of non-const reference with C++11 thread?

The specification for the std::thread constructor says

Effects: Constructs an object of type thread. The new thread of execution executes INVOKE ( DECAY_COPY ( std::forward<F>(f)), DECAY_COPY (std::forward<Args>(args))...) with the calls to DECAY_COPY being evaluated in the constructing thread.

Where DECAY_COPY(x) means calling decay_copy(x) where that is defined as:

template <class T> typename decay<T>::type decay_copy(T&& v)
{ return std::forward<T>(v); }

What this means is that the arguments "decay" and are copied, meaning that they are forwarded by value and lose any cv-qualification. Because the target function run by the thread wants to take its parameter by reference, you get a compiler error saying the reference cannot bind to an object passed by value.

This is by design, so that by default local variables passed to a std::thread get passed by value (i.e. copied) not by reference, so that the new thread will not have dangling references to local variables that go out of scope, leading to undefined behaviour.

If you know it's safe to pass the variables by reference then you need to do so explicitly, using a reference_wrapper which will not be affected by the "decay" semantics, and will forward the variable by reference to the target object. You can create a reference_wrapper using std::ref.

Invalid initialization of non-const reference of type

Simply make your print function take your argument by const&. This is also logically right as it doesn't modify your argument.

void printAge(const Person &person) {
cout << "Age: " << person.age << endl;
}

The actual problem is the other way around. You are passing a temporary(rvalue) to a function which expects an lvalue.

invalid initialization of non-const reference of type 'A&' from an rvalue of type 'A'

Because the constructor of X takes a reference, and you can't pass a temporary by reference.

To fix it, make X constructor take a const reference. And then, fun and fun2 should be made const for the code to compile.

invalid initialization of non-const reference of type ‘Node*&’ from an rvalue of type ‘std::atomicNode*::__pointer_type {aka Node*}’

Simple answer is that temp1 should be a Node*, not an Atomic,
as cmp/xchg takes two simple type variables.

But I don't really understand what you are trying to achieve.
Surely if you want next to be protected against threading, then it should be declared Atomic inside the struct?

error: invalid initialization of non-const reference of type 'std::functionvoid()&' from an rvalue of type 'main()::lambda()'|

In printfunction call, lambda expression [value]() {...} argument must be converted to a temporary function<void()> object first.

A reference to non-const function<void()>& only binds to l-values, not temporaries (r-values).

A reference to const, on the other hand, can be bound to temporaries. Which is what you observe.

invalid initialization of non-const reference of type ' ' from an rvalue of type ' '

The problem is not in your RefC class but in the declaration of the insertar() function. I guess it looks like

insertar( RefC& r);

but you want to pass a temporary object (rvalue), which is only possible if the parameter type is const-ref

insertar( const RefC& r);

rvalue means a value that can only be on the right side of an assignment, e.g. x = y, x must be an lvalue, usually something with a name, something to which a value can be assigned to, whereas an rvalue (y) can be a constant, a literal, a temporary object etc.

invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’: Recursive pass-by-reference function

Note that the result returned by operator- are rvalues; so -beta and -alpha are both rvalues and then can't be bound to lvalue-reference to int, i.e. int&.

You can pass lvalues instead; if you don't want to change the parameter type of the function (and the arguments are supposed to be modified for pass-by-reference). e.g.

int search(int& alpha, int& beta, int h){
if(h == 0){
return value();
}
//do some stuff
alhpa = -alpha;
beta = -beta;
int score = search(beta, alpha, h-1);
//do some more stuff
return score;
}

invalid initialization of non-const reference of type std::string& - how to remove

Neither one of those strings should be a reference. They're objects, pure and simple. Remove the two &s.

invalid initialization of non-const reference of type 'int&', what the reason?

The postfix increment operator on an int returns a temporary value. A temporary value cannot bind to a non-const lvalue reference, because modifying that temporary doesn't make sense. You are trying to bind the temporary to an int&, which is giving an error.

To fix this, either use the pre-increment operator (++a), or take your argument by value (it's better to pass builtin types as value rather than const T&):

int f(int a)
{
return a;
}

What does `invalid initialization of non-const reference` mean?

In C++ temporaries cannot be bound to non-constant references.

Main<int> &mainReference = Main<int>::tempFunction();

Here you are trying to assign the result of an rvalue expression to a non-constant reference mainReference which is invalid.

Try making it const



Related Topics



Leave a reply



Submit