【发布时间】:2016-07-14 10:34:31
【问题描述】:
在my previous question 继续工作时,我遇到了 clang 和 GCC 的不同行为。
我需要检查成员函数指针,因为我需要知道该函数是否被继承。
在 SFINAE 上下文中比较成员函数指针时,成员函数 Foo::foo() 存在,但其主体包含最终无法编译的代码 (x.hello())。
以下代码使用 clang 编译。
然而,GCC 似乎评估了 Foo::foo() 的函数体并以错误 ('struct Caller' has no member named 'hello') 退出,尽管处于未评估的 SFINAE 上下文中(或者我希望如此)。
#include <iostream>
#include <type_traits>
struct Foo
{
template <typename T> void foo(T&& x) { x.hello(); }
};
struct Caller
{
template <typename T>
auto call(T&& x) -> decltype(
std::enable_if_t<
std::is_same<
decltype(&T::template foo<decltype(*this)>),
void (T::*)(decltype(*this))
>::value
>())
{
//x.foo(*this);
}
};
int main()
{
Caller c;
c.call(Foo());
}
我测试过:
- clang 3.8.0
- g++ 6.1.0
两者的编译器选项:-std=c++14 -O2 -Wall -pedantic -pthread
我的问题:
- 谁是对的? Clang 还是 GCC?
- 如何获取代码以使用 GCC 编译?
【问题讨论】:
-
提一下编译器的版本和选项怎么样?
x.hello()绝对不在 SFINAE 上下文中。
标签: c++ templates gcc sfinae clang++