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