如何使用std :: copy将constexpr数组复制到另一个constexpr数组? [英] How do you copy a constexpr array to another constexpr array with std::copy?

查看:123
本文介绍了如何使用std :: copy将constexpr数组复制到另一个constexpr数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我创建了一个长度为6的数组,并在前3个元素中用1、2和3对其进行了初始化。然后,我将前3个元素复制到后3个元素。然后我依次打印所有元素。

In the following code, I create an array of length 6 and initialize it with 1, 2 and 3 in the first 3 elements. Then I copy the first 3 elements to the last 3 elements. Then I print all the elements in order.

std::array<int, 6> bar = {1, 2, 3};

int main(){
    // Copy the first 3 elements to the last 3 elements
    std::copy(bar.begin(), bar.end() - 3, bar.end() - 3);

    // Print all the elements of bar
    for(auto& i: bar) std::cout << i << std::endl;
}

它工作正常,但是当我尝试使数组 constexpr 它不再编译:

It works fine, but when I try to make the array constexpr it no-longer compiles:

constexpr std::array<int, 6> bar = {1, 2, 3};

int main(){
    // Copy the first 3 elements to the last 3 elements
    std::copy(bar.begin(), bar.end() - 3, bar.end() - 3); // Won't compile!

    // Print all the elements of bar
    for(auto& i: bar) std::cout << i << std::endl;
}

使用 g ++ -std = c ++ 14编译main.cpp -o main 我收到以下错误消息:

Compiling with g++ -std=c++14 main.cpp -o main I get the following error message:

/usr/include/c++/5/bits/stl_algobase.h: In instantiation of ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = const int*; _OI = const int*]’:
/usr/include/c++/5/bits/stl_algobase.h:438:45:   required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = const int*; _OI = const int*]’
/usr/include/c++/5/bits/stl_algobase.h:471:8:   required from ‘_OI std::copy(_II, _II, _OI) [with _II = const int*; _OI = const int*]’
main.cpp:115:53:   required from here
/usr/include/c++/5/bits/stl_algobase.h:402:44: error: no matching function for call to ‘std::__copy_move<false, true, std::random_access_iterator_tag>::__copy_m(const int*&, const int*&, const int*&)’
                        _Category>::__copy_m(__first, __last, __result);
                                            ^
/usr/include/c++/5/bits/stl_algobase.h:373:9: note: candidate: template<class _Tp> static _Tp* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(const _Tp*, const _Tp*, _Tp*) [with _Tp = _Tp; bool _IsMove = false]
         __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
         ^
/usr/include/c++/5/bits/stl_algobase.h:373:9: note:   template argument deduction/substitution failed:
/usr/include/c++/5/bits/stl_algobase.h:402:44: note:   deduced conflicting types for parameter ‘_Tp’ (‘int’ and ‘const int’)
                        _Category>::__copy_m(__first, __last, __result);

我完全不理解此错误消息。 std :: copy 不是 constexpr 吗?如果不是,应该是吧?如果 std :: copy constexpr ,我的代码是否可以工作?

I don't understand this error message at all. Is std::copy not constexpr? If it isn't, it should be, right? Does my code would work if std::copy was constexpr?

推荐答案

您应该创建constexpr函数。 constexpr 表示const,但不在 constexpr 函数的范围内。

You should make a constexpr function. constexpr imply const, but not inside the scope of a constexpr function.

constexpr auto get_bar() {
    std::array<int, 6> bar = {1, 2, 3, 0, 0, 0};

    copy(bar.begin(), bar.end() - 3, bar.end() - 3);

    return bar;
}

但是,您将需要编写自己的 copy ,因为它在标准库中未标记为 constexpr

However, you will need to write your own version of copy because it's not marked as constexpr in the standard library.

编译时间数组的值没有意义,就像要求编译器在运行时更改变量的类型一样。运行时甚至不存在编译器。但是,constexpr函数是由编译器执行的,因此要求它更改值仍然有意义。这就是上面的代码有意义的原因。

Changing the value of a compile time array does not make sense, it's the same thing as asking the compiler to change the type of a variable at runtime. The compiler don't even exist during runtime. However, a constexpr function is executed by the compiler, so it's still make sense to ask it to mutate values. This is why the code above make sense.

请注意,大多数 std :: array 访问器都不是 constexpr 直到C ++ 17。

Note that most of std::array accessors are not constexpr until C++17.

这篇关于如何使用std :: copy将constexpr数组复制到另一个constexpr数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆