我有一个模板化成员函数,如下所示:
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 report
最佳答案
3.9[basic.types]/9
说
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
您的情况是标量类型的 cv 限定版本的数组。
关于c++ - const int[2] 可以轻易复制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19526526/