简体   繁体   中英

could not deduce template argument in VS 2010

In VS 2005 this code work fine, but in VS 2010 I have error "could not deduce template argument for 'T *' from 'std::queue<_Ty> *'"

I can't understand what the problem is? Please, help me...

#include <string>
#include <queue>
using namespace std;
template<typename  T, typename  R, typename  P1>
int bindthis(T* obj, R (T::*func)(P1))
{
    return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::queue<std::wstring> queue_;

bindthis(&queue_, &std::queue<std::wstring>::push);
return 0;
}

I'm not sure about Visual Studio, but in GCC this function compiles in C++03 mode but not in C++11 mode, so I imagine the problem is the same.

The issue is that in C++11, an overload was added to std::queue::push , so the compiler doesn't know which overload to pick. There are two ways to fix this:

  1. Specify the template arguments explicitly:

     bindthis<std::queue<std::wstring>, void, const std::wstring&>(&queue_, &std::queue<std::wstring>::push); 
  2. Cast the function pointer to the desired type void (std::queue<std::wstring>::*)(const std::wstring&) , so that the correct overload is chosen:

     typedef void (std::queue<std::wstring>::*push_func_ptr)(const std::wstring&); bindthis(&queue_, static_cast<push_func_ptr>(&std::queue<std::wstring>::push)); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM