c++ - const int[2] 可以轻易复制吗?

标签 c++ visual-studio-2012 c++11 visual-studio-2013 static-assert

我有一个模板化成员函数,如下所示:

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/

相关文章:

c++ - 大括号中的类名后面是什么? C++

c++ - MultiByteToWideChar 终止带有垃圾的输出缓冲区,但未报告任何错误。为什么?

c++ - bool 表达式作为模板参数

c++ stol 抛出字符串长度为 10 或更长的超出范围异常

c++ - 在分配的地址实例化结构(分配赋值)

C++析构函数问题

c++ - move 后使用 unique_ptr

c++ - 结构可以在方法体内声明,但前提是它不包含成员字段初始值设定项。编译器错误与否?

c# - 带 SSIS 包的 Winscp 给出 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)

c++ - 标准如何支持在基类 S 中调用纯虚函数?