gpt4 book ai didi

c++ - const int[2] 是否可以简单复制?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:25:48 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com