Vishaal Kalwani
Vishaal Kalwani

Reputation: 740

Strange compiler error when trying to construct std::thread:

The following program fails to compile with g++ -std=c++11 -Wall -Werror test.cpp -o test.o:

#include <thread>
using namespace std;

void fill(int n) {
    return;
}

int main() {
    thread test(fill, 5);
}
test.cpp:9:12: error: no matching constructor for initialization of 'std::__1::thread'
    thread test(fill, 5);
           ^    ~~~~~~~

Is it because fill is conflicting with std::fill from #include <algorithm>? I haven't included this but I suppose <thread> might've.

Changing my function name to fillie (or anything else pretty much) allows it to compile correctly without linking pthread.

I ask because it is a strange compiler error message, and also it means that the thread constructor can't disambiguate which function I am using based on the parameters (which sort of makes sense, but wanted confirmation).

Upvotes: 0

Views: 453

Answers (1)

John Zwinck
John Zwinck

Reputation: 249153

Yes, the problem is because it is not known whether fill is std::fill or your global fill function.

One way to fix it is to write ::fill to explicitly use the global one.

Upvotes: 5

Related Questions