Is const int[2] trivially copyable?

别来无恙 提交于 2019-12-08 01:01:55

问题


I have a templated member function which looks somewhat like the following:

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
    ...
}

I then call this function in a manner like the following:

const int iArray[2] = {1, 2};
int iResult = pSocket->SendData( iArray );

When I compile this with Visual Studio 2012, I get no error message and the functionality of the program is how I would expect (i.e. the data is sent bitwise), however, when compiling with the newest version of the compiler, Visual Studio 2013, the static assert fails, the compiler issuing me the statement:

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>          ]

So which version of the compiler is standards-conformant, should const int[2] be trivially copyable or not?


Edit: This is a bug with Visual Studio 2013; here is the Microsoft Connect report


回答1:


3.9[basic.types]/9 says

Scalar types, trivially copyable class types (Clause 9), arrays of such types, and cv-qualified versions of these types (3.9.3) are collectively called trivially copyable types

Your case is array of cv-qualified version of a scalar type.




回答2:


Yes.

Scalar types and arrays of TriviallyCopiable objects are TriviallyCopiable as well. [1]

Also gcc reports it as trivially copiable. It seems a bug in VS2013.



来源:https://stackoverflow.com/questions/19526526/is-const-int2-trivially-copyable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!