相关疑难解决方法(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
查看次数

如何写`is_complete`模板?

回答完这个问题后,我试图is_complete在Boost库中找到模板,我意识到Boost.TypeTraits中没有这样的模板.为什么Boost库中没有这样的模板?应该怎么样?

//! Check whether type complete
template<typename T>
struct is_complete
{   
  static const bool value = ( sizeof(T) > 0 );
};

...

// so I could use it in such a way
BOOST_STATIC_ASSERT( boost::is_complete<T>::value );
Run Code Online (Sandbox Code Playgroud)

上面的代码不正确,因为应用于sizeof不完整类型是非法的.什么是好的解决方案?在某种程度上可以在这种情况下应用SFINAE吗?


嗯,这个问题一般不会在不违反ODR规则的情况下解决,但是有一个特定于平台的解决方案对我有用.

c++ templates type-traits

21
推荐指数
4
解决办法
3303
查看次数

标签 统计

c++ ×2

templates ×2

c++11 ×1

clang ×1

gcc ×1

type-traits ×1