为什么编译器不能在我的代码中推断出模板参数?

时间:2011-07-04 16:20:23

标签: c++ templates c++11 member-function-pointers visual-studio-2010-sp1

我正在使用视觉工作室,我已经尝试了所有我能想到的东西。但不知道为什么这段代码会产生错误,这是我的代码:

template <class A,class B> B returnArgtype(void (A::*)(B)) {return *new B;}

struct test
{
    void function(int);
    decltype(returnArgtype(&test::function)) x;
};

并生成此错误:

error C2784: 'A returnArgtype(void (__thiscall A::* )(B))' : could not deduce template argument for 'void (__thiscall A::* )(B)' from 'void (int)'

我想知道当参数x在函数内部初始化时它不会产生错误,如下所示:

struct test
{
    void function(int)
    {
        decltype(returnArgtype(&test::function)) x;
    }
};

2 个答案:

答案 0 :(得分:2)

这对我有用(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;
};

答案 1 :(得分:1)

这与我在其他问题上链接的错误相同(请提示它以使MS更有可能花时间修复它):

C++ compiler loses member-ness of pointer-to-member-function during template deduction, causes ICE

然后,看看@Ise Wistera's answer哪个更简单,可能不会导致这个问题。


微软更新了错误报告,说他们已经找到了解决办法。

相关问题