为什么编译器无法推断出我的代码中的模板参数?
我正在使用 Visual Studio,并且尝试了所有我能想到的方法。但不知道为什么这段代码会生成错误,这是我的代码:
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;
}
};
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;
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这与我在您的其他问题上链接到的错误相同(请对其进行投票,以便 MS 更有可能花时间修复它):
C++ 编译器在模板推导过程中丢失指向成员函数指针的成员性,导致 ICE
然后,查看 @Ise Wistera 的答案 更简单,可能不会导致此问题。
微软更新了错误报告,表示他们已经找到了修复方法。
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.
这对我有用(GCC 4.6,
-std=c++0x
):This works for me (GCC 4.6,
-std=c++0x
):