gpt4 book ai didi

c++ - std::swap 不适用于 std::array

转载 作者:太空狗 更新时间:2023-10-29 20:02:41 24 4
gpt4 key购买 nike

我有一些代码要求我将一些数据放入 std::array .我想我可以通过交换两个数组并丢弃其中一个来做到这一点。这是代码

int main()
{
std::array<double, 10> a;
std::array<double, 5> b;
/*populate b*/
/*swap them round*/
std::swap(a, b);
}

但是我得到一个非常奇怪的编译器错误 (MSVC2013)。

CashFlows.cpp(27): error C2665: 'std::swap' : none of the 3 overloads could convert all the argument types
include\exception(502): could be 'void std::swap(std::exception_ptr &,std::exception_ptr &)'
include\tuple(572): or 'void std::swap(std::tuple<> &,std::tuple<> &)'
include\thread(232): or 'void std::swap(std::thread &,std::thread &) throw()'
while trying to match the argument list '(std::array<_Ty,_Size>, std::array<_Ty,_Size>)'
with
[
_Ty=double,
_Size=0x0a
]
and
[
_Ty=double,
_Size=0x05
]

我不明白。什么是std::tuple<>等与此有什么关系?

最佳答案

对象 ab根本上不同的类型,因为它们是不同的模板实例化。

因此您不能在 std::swap 中一起使用它们,因为参数必须是同一类型。

您的编译器找不到合适的重载(当然,它向您显示的重载看起来确实很奇怪),因此发出您看到的错误。

关于c++ - std::swap 不适用于 std::array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37618471/

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