在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' …Run Code Online (Sandbox Code Playgroud) 假设我有一些模板类,具体取决于类型T. T可能是几乎所有的东西:int,int*,pair <int, int>或struct lol; 它不能void,参考或任何cv资格但是.对于某些优化,我需要知道我是否可以继承T.所以,我需要一些特质类型is_subclassable,确定为基本特征的逻辑组合或通过一些SFINAE技巧.
在原始示例中,int并且int*不是子类,pair <int, int>而且struct lol是.
编辑:正如下面指出的litb,工会也不是可子类化的,T也可以是联合类型.
如何编写我需要的特征类型?