const int[2] 是微不足道的可复制的

Is const int[2] trivially copyable?

本文关键字:微不足道 可复制 int const      更新时间:2023-10-16

我有一个模板化的成员函数,它看起来有点像下面:

template <typename T>
int SendData( const T& tDataBuffer ) const
{
    static_assert( std::is_trivially_copyable<T>::value, "The object type must be trivially copyable" );
    // Send the data bitwise
    ...
}

然后,我以如下所示的方式调用此函数:

const int iArray[2] = {1, 2};
int iResult = pSocket->SendData( iArray );
当我使用 Visual Studio

2012 编译它时,我没有收到任何错误消息,并且程序的功能是我期望的(即数据按位发送),但是,当使用最新版本的编译器 Visual Studio 2013 进行编译时,静态断言失败,编译器向我发出语句:

1>c:...sockets.h(298): error C2338: The object type must be trivially copyable
1>          c:...test.cpp(974) : see reference to function template instantiation 'int CBaseSocket::SendData<const int[2]>(T (&)) const' being compiled
1>          with
1>          [
1>              T=const int [2]
1>          ]

那么哪个版本的编译器符合标准,const int[2]是否应该可以复制?


编辑:这是Visual Studio 2013的错误;这是Microsoft Connect报告。

3.9[basic.types]/9

标量类型、可平凡复制的类类型(条款 9)、此类类型的数组以及这些类型的 cv 限定版本(3.9.3)统称为平凡可复制类型

您的案例是标量类型的 cv 合格版本的数组。

是的。

平凡可复制对象的标量类型和数组是 也可以微不足道地复制。[1]

此外,gcc 将其报告为微不足道的可复制性。这似乎是VS2013中的一个错误。