Can't bind (function pointer) lvalue to (function pointer) rvalue when std :: thread starts?

I have an interesting problem with gcc 4.5.2. Following code

#include<thread>
#include<iostream>

using std::cout;
void foo(int a){
    cout<<a;
}

template <typename T>
void goo(void (*fn)(T),T c){
    fn(c);
}

int main(void)
{
    std::thread TH;
    void (*ptr)(int)=foo;
    TH= std::thread(goo<int>,ptr,1);
    TH.join();

    return 0;
}

.. will not compile on gcc 4.5.2 s error: cannot bind ‘void(void (*)(int), int)’ lvalue to ‘void (&&)(void (*)(int), int)’followed by a second errorinitializing argument 1 of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void(void (*)(int), int), _Args = {void (*&)(int), int}]’

However, this code compiles when it templateis deleted, and also compiles with gcc 4.7.0 (with templatein place).

Even if this is a compiler problem, can someone explain what the error is? I would be happy to find a way to do the binding (even if it is, say, automatic in gcc 4.7).

+3
source share
2 answers

You stumbled upon two errors.

​​GCC , " " , goo<int> rvalue. goo<int> lvalue. _Callable void(void (*)(int), int) void(&)(void (*)(int), int). , lvalue void(&&)(void (*)(int), int), GCC.

rvalue lvalue ( , GCC4.5, , , ). rvalue l .

lvalue , , GCC (. http://llvm.org/bugs/show_bug.cgi?id=7505 http://llvm.org/bugs/show_bug.cgi?id=7505 , ).

+5

lvalue , , . , lvalue: int i; i = 3; , 5 = 3 - . , &i , &5 .

goo<int> - lvalue , C ++. , . lvalue, .

g++ , . -, - goo, goo<int>. , , .

& :

TH= std::thread(&goo<int>,ptr,1);

http://ideone.com/mI05j

+4

All Articles