1

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::*)(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;
    }
};
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
Ali1S232
  • 3,373
  • 2
  • 27
  • 46

2 Answers2

2

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;
};
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • It seems it only has problem on visual studio, I've tested that code on GCC myself and I didn't encounter any problems. I was looking for some answer to fix that in Visual studio. – Ali1S232 Jul 07 '11 at 18:39
1

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.

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720