MSVC12认为从std :: array派生的聚合不是pod [英] MSVC12 thinks aggregate derived from std::array is not pod

查看:134
本文介绍了MSVC12认为从std :: array派生的聚合不是pod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下

#include <array>

struct litmus final : std::array<unsigned char, 16>
{
};

static_assert(std::is_pod<std::array<unsigned char, 16> >::value, "not pod");

// this fails on MSVC:
static_assert(std::is_pod<litmus>::value, "not pod");

以下编译器同意 litmus

  • clang++ version 3.5 (trunk 198621) http://coliru.stacked-crooked.com/a/7add7a2fe58a7e38
  • g++ 4.8.1 http://coliru.stacked-crooked.com/a/74cfe97f06c8c128

但是,MSVC12(VS2013 RTM)认为第二个断言失败。

However, MSVC12 (VS2013 RTM) maintains that the second assert fails.


  1. / li>
  2. 有什么技巧可以让MSVC将该类视为pod?




EDIT 有关信息: is_trivially_copyable< litmus> 在MSVC上返回true。这在很多不严格要求实际POD的情况下可能是有用的。

EDIT For information: is_trivially_copyable<litmus> returns true-ness on MSVC. This might be useful for many cases where actual POD-ness isn't strictly required.


推荐答案


  1. GCC和Clang;该类是POD。

  1. GCC and Clang; that class is POD.

这取决于确切是指make MSVC将类作为pod。如果你的意思是编译器内部,那么没有。但是,您可以(在实践中)专门处理 litmus 的特性:

That depends on what exactly you mean with "make MSVC treat the class as pod." If you mean the compiler internals, then no. However, you can (in practice) specialise the trait for litmus:

namespace std {

template <>
struct is_pod<litmus> : std::true_type
{};

}

请注意,严格遵循标准,这会给出未定义的行为(感谢@ R.MartinhoFernandes指出这一点)。但是,作为一个特定于编译器的解决方法,我希望它能工作。无任何保证。

Note that going strictly by the standard, this gives undefined behaviour (thanks @R.MartinhoFernandes for pointing this out). However, as a compiler-specific workaround, I'd expect it to work. Use without any warranty.

这篇关于MSVC12认为从std :: array派生的聚合不是pod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆