相关疑难解决方法(0)

模板别名和sfinae

如果替换失败涉及模板别名(例如,缺少的成员类型名称上的模板别名,如下面的代码片段中所示),是否应触发错误?

Clang和gcc似乎对此持不同意见:

// some types
struct bar { };

struct foo {
    typedef void member_type;
};


// template alias
template<class T>
using member = typename T::member_type;


template<class T>
void baz(... ) { }

// only works for gcc, clang fails with: no type named 'member_type'
// in 'bar'
template<class T>
void baz( member<T>* ) { }


int main(int, char** ) {

    baz<bar>(0);            // picks first
    baz<foo>(0);            // picks second

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以问题是:谁是正确的,为什么?

谢谢 :-)

c++ sfinae c++11 template-aliases

10
推荐指数
1
解决办法
945
查看次数

标签 统计

c++ ×1

c++11 ×1

sfinae ×1

template-aliases ×1