user2018734
user2018734

Reputation: 13

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;
}

Upvotes: 1

Views: 368

Answers (1)

interjay
interjay

Reputation: 110191

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));
    

Upvotes: 3

Related Questions