在 C++0x 中,SFINAE 规则已被简化,因此在演绎的“直接上下文”中出现的任何无效表达式或类型都不会导致编译器错误,而是会导致演绎失败 (SFINAE)。
我的问题是:
如果我取了一个重载函数的地址并且它不能被解析,那是在直接推理的上下文中失败吗?
(即如果无法解决,是硬错误还是 SFINAE)?
这是一些示例代码:
struct X
{
// template<class T> T* foo(T,T); // lets not over-complicate things for now
void foo(char);
void foo(int);
};
template<class U> struct S
{
template<int> struct size_map
{ typedef int type; };
// here is where we take the address of a possibly overloaded function
template<class T> void f(T,
typename size_map<sizeof(&U::foo)>::type* = 0);
void f(...);
};
int main()
{
S<X> s;
// should this cause a compiler error because 'auto T = &X::foo' is invalid?
s.f(3);
}
Gcc 4.5 声明这是一个编译器错误,并且 clang 吐出了一个断言违规。
以下是一些更相关的感兴趣的问题:
FCD-C++0x 是否清楚地指定了这里应该发生什么?
编译器拒绝此代码是否错误?
是否需要更好地定义演绎的“直接上下文”?
谢谢!