why compiler can't deduce template argument in my code?
I'm using visual studio and I've tried every thing I could think of. but don't know why this piece of code generates error, this is my code:
template <class A,class B> B returnArgtype(void (A::开发者_如何学Python*)(B)) {return *new B;}
struct test
{
void function(int);
decltype(returnArgtype(&test::function)) x;
};
and it generates this error :
error C2784: 'A returnArgtype(void (__thiscall A::* )(B))' : could not deduce template argument for 'void (__thiscall A::* )(B)' from 'void (int)'
and I'm wondering it doesn't generate that error when parameter x is initializing inside a function, something like this:
struct test
{
void function(int)
{
decltype(returnArgtype(&test::function)) x;
}
};
This works for me (GCC 4.6, -std=c++0x
):
template <class A, class B> B returnArgtype(void (A::*)(B));
struct test
{
void function(int);
decltype(returnArgtype(&test::function)) x;
};
This is the same bug I linked to on your other question (please upvote it to make it more likely MS will spend time fixing it):
C++ compiler loses member-ness of pointer-to-member-function during template deduction, causes ICE
Then, look at @Ise Wistera's answer which is much simpler and probably doesn't cause this problem.
Microsoft updated the bug report to say they've figured out a fix.
精彩评论