相关疑难解决方法(0)

什么是"表达SFINAE"?

http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx上,VC++团队正式声明他们尚未实现C++ 11核心功能"Expression SFINAE".但是,从http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2634.html复制的以下代码示例将被VC++编译器接受.

例1:

template <int I> struct A {};

char xxx(int);
char xxx(float);

template <class T> A<sizeof(xxx((T)0))> f(T){}

int main()
{
    f(1);
}
Run Code Online (Sandbox Code Playgroud)

例2:

struct X {};
struct Y 
{
    Y(X){}
};

template <class T> auto f(T t1, T t2) -> decltype(t1 + t2); // #1
X f(Y, Y);  // #2

X x1, x2;
X x3 = f(x1, x2);  // deduction fails on #1 (cannot add X+X), calls #2
Run Code Online (Sandbox Code Playgroud)

我的问题是:什么是"表达SFINAE"?

c++ templates sfinae visual-c++ c++11

56
推荐指数
1
解决办法
1万
查看次数

std :: is_function无法将模板参数识别为函数

我将函数指针传递给函数模板:

int f(int a) { return a+1; }

template<typename F>
void use(F f) {
    static_assert(std::is_function<F>::value, "Function required"); 
}

int main() {
    use(&f); // Plain f does not work either.
}
Run Code Online (Sandbox Code Playgroud)

但模板参数F不被识别is_function为函数,静态断言失败.编译器错误消息说,F就是int(*)(int)这是一个函数指针.为什么它表现得那样?在这种情况下,如何识别功能或指针?

c++ types c++11

11
推荐指数
1
解决办法
1193
查看次数

标签 统计

c++ ×2

c++11 ×2

sfinae ×1

templates ×1

types ×1

visual-c++ ×1