相关疑难解决方法(0)

我的is_complete类型特征的实现是否暴露了编译器错误?

我编写了这个C++ 11特征模板来检查类型是否完整:

template <typename...>
using void_t = void;

template <typename T, typename = void>
struct is_complete : std::false_type
{};

template <typename T>
struct is_complete<T, void_t<decltype(sizeof(T))>> : std::true_type
{};
Run Code Online (Sandbox Code Playgroud)

并测试它像这样:

struct Complete {};

int main()
{
    std::cout << is_complete<Complete>::value
              << is_complete<class Incomplete>::value
              << '\n';
}
Run Code Online (Sandbox Code Playgroud)

我希望打印测试程序10,这是我用clang 3.4编译它时得到的输出.但是,当使用gcc 4.9编译时,它会打印11- 错误地识别class Incomplete为完整.

我不确定我的代码是否正确,但在我看来,即使它是错误的,它在两个编译器上也应该表现相同.

问题1:我的代码是否正确?
问题2:我是否在其中一个编译器中发现了错误?

编辑:

我不是要求替换我的代码.我问的是gcc或clang中是否存在错误,以及这个特定结构是否正确.

c++ gcc templates clang c++11

26
推荐指数
1
解决办法
911
查看次数

标签 统计

c++ ×1

c++11 ×1

clang ×1

gcc ×1

templates ×1