Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to specialize function template in threads

I'm trying to implement a Thread in c++ wich it will be used to control when I take a picture, but this simple code is giving me a strange error, which I can't understand. This is my code:

#include <iostream>
#include <fstream>
#include <thread>
    using namespace std;

    void counter(int seconds, bool &flagTakePhoto, bool &flagThreadStart);

int main() {
    bool takePhoto, threadStart;

    int seconds = 1;

    thread t(counter, seconds, takePhoto, threadStart);
    //Some code here
}


void counter(int seconds, bool &flagTakePhoto, bool &flagThreadStart) {
    while (flagThreadStart) {
        this_thread::sleep_for(chrono::seconds(seconds));
        flagTakePhoto = true;
    }
    terminate();
}

This is the error:

1>------ Build started: Project: Proyecto para pruebas OPENCV, Configuration: 
Release x64 ------
1>  Main.cpp
1>Main.cpp(46): warning C4244: 'initializing': conversion from '__int64' to 'int', possible loss of data
1>C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): error C2893: Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): note: With the following template arguments:
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): note: '_Callable=void (__cdecl *)(int,bool &,bool &)'
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(238): note: '_Types={int, bool, bool}'
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(247): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0x00,0x01,0x02,0x03>(std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool> &,std::integer_sequence<_Ty,0x00,0x01,0x02,0x03>)' being compiled
1>          with
1>          [
1>              _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>,
1>              _Ty=size_t
1>          ]
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(247): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Execute<0x00,0x01,0x02,0x03>(std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool> &,std::integer_sequence<_Ty,0x00,0x01,0x02,0x03>)' being compiled
1>          with
1>          [
1>              _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>,
1>              _Ty=size_t
1>          ]
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(242): note: while compiling class template member function 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept'
1>          with
1>          [
1>              _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1>          ]
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(230): note: see reference to function template instantiation 'void std::_LaunchPad<_Target>::_Run(std::_LaunchPad<_Target> *) noexcept' being compiled
1>          with
1>          [
1>              _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1>          ]
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thr/xthread(256): note: see reference to class template instantiation 'std::_LaunchPad<_Target>' being compiled
1>          with
1>          [
1>              _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1>          ]
1>  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\thread(52): note: see reference to function template instantiation 'void std::_Launch<std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>>(_Thrd_t *,_Target &&)' being compiled
1>          with
1>          [
1>              _Target=std::unique_ptr<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>,std::default_delete<std::tuple<void (__cdecl *)(int,bool &,bool &),int,bool,bool>>>
1>          ]
1>  Main.cpp(136): note: see reference to function template instantiation 'std::thread::thread<void(__cdecl &)(int,bool &,bool &),int&,bool&,bool&,void>(_Fn,int &,bool &,bool &)' being compiled
1>          with
1>          [
1>              _Fn=void (__cdecl &)(int,bool &,bool &)
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

What is more strange about this is that I have a similar code in other program and compile with no errors! Does anyone knows what's wrong with this code? Thanks!

like image 989
Isaac Avatar asked Sep 26 '22 15:09

Isaac


1 Answers

The std::thread constructor will construct all of its objects and call the function as if by:

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

That makes the argument types of

thread t(counter, seconds, takePhoto, threadStart);

be int, bool, bool. Since you cannot call counter(int, bool, bool) (it takes lvalue references), that constructor is ill-formed.

In order to pass references, you need to wrap your arguments in std::reference_wrapper<T>. That type is implicitly convertible to T&, so calling counter(int, std::reference_wrapper<bool>, std::reference_wrapper<bool>) is valid. For short-hand, the standard provides std::ref, so you can just do the following:

thread t(counter, seconds, std::ref(takePhoto), std::ref(threadStart));
like image 120
Barry Avatar answered Nov 15 '22 11:11

Barry