Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicitly convert to reference

Tags:

c++

I have:

struct vec {
    __m128 m128;
    inline vec(__m128 m128) : m128(m128) {
    }
}

so now __m128 can implicitly convert to vec, but when I use it, as in:

void doStuff(vec &v) { *stuff be doing* }
doStuff( _mm_set1_ps(1.0f)); //mm_set_ps returns __m128

I get an error saying:

Can't convert from __m128 to &vec

so what's the problem and how to fix it?

like image 267
Thomas Avatar asked Jul 20 '15 11:07

Thomas


1 Answers

doStuff takes in a reference to a non-const vec. Non-const references cannot bind to rvalues like the result of a function call.

If you need to modify v inside doStuff, then store the result of _mm_set1_ps(1.0f) in an intermediate variable, then call with that:

vec v = _mm_set1_ps(1.0f);
doStuff(v);

If you don't need to modify v, change doStuff to take its argument by reference-to-const:

void doStuff(const vec &v) { /*stuff doing*/ }
like image 187
TartanLlama Avatar answered Sep 26 '22 20:09

TartanLlama