类模板的成员模板函数上的enable_if
这似乎是 MSVC10 中的一个错误?
#include <type_traits>
template<int j>
struct A{
template<int i>
typename std::enable_if<i==j>::type
t(){}
};
int main(){
A<1>().t<1>(); //error C2770
}
错误 C2770:无效的显式 template_or_generic 参数“enable_if::type A::t(void)”。
编译如下:
#include <type_traits>
template<class j>
struct A{
template<class i>
typename std::enable_if<std::is_same<i,j>::value>::type
t(){}
};
template<unsigned int j>
struct B{
template<unsigned int i>
typename std::enable_if<i==j>::type
t(){}
};
int main(){
A<int>().t<int>();
B<1>().t<1>();
}
This seams to be a bug in MSVC10?
#include <type_traits>
template<int j>
struct A{
template<int i>
typename std::enable_if<i==j>::type
t(){}
};
int main(){
A<1>().t<1>(); //error C2770
}
error C2770: invalid explicit template_or_generic argument(s) "enable_if::type A::t(void)".
The following compiles:
#include <type_traits>
template<class j>
struct A{
template<class i>
typename std::enable_if<std::is_same<i,j>::value>::type
t(){}
};
template<unsigned int j>
struct B{
template<unsigned int i>
typename std::enable_if<i==j>::type
t(){}
};
int main(){
A<int>().t<int>();
B<1>().t<1>();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎是 MSVC2010 方面的一些奇怪行为,它无法确定您是否使用 <1> 。作为模板参数是基于 int 的模板的实例化。
当我编译上面的代码时,我收到以下详细错误:
如果将 1 值交换为 0,您会发现它仍然不起作用,但如果您使用任何其他有效的 int,模板似乎编译得相当愉快。
我不完全确定为什么会发生这种情况,但是您可以通过使用 const ints 来表示模板参数来使此代码工作:
基于此,我的怀疑是编译器发现 0 和 1 的使用在出现时不明确到模板实例化。希望这里的解决方法对通过谷歌偶然发现这个问题的人有帮助......
This appears to be some strange behaviour on the part of MSVC2010 where it is unable to make a determination as to whether your use of <1> as a template parameter is an instantiation of an int-based template.
When I compile your code above, I get the following, verbose error:
If you swap your 1 values out for 0, you find it still doesn't work, but if you use any other valid int, the template appears to compile quite happily.
I'm not entirely sure why this is happening, but you can make this code work by using const ints to represent the template parameters:
My suspicion, based on this, is that the compiler finds the use of 0 and 1 ambiguous when it comes to template instantiation. Hopefully the workaround here is helpful to someone stumbling across this through google...