返回 std::array<std:tuple<uint32_t, uint32_t, uint32_t>, size_t>
的 Constexpr 函数由于使用了 std::make_tuple,因此在编译时不起作用。有什么办法可以克服这个问题吗?
当我尝试删除 constexpr 规范时。它工作正常。然而,我们项目的目标是在编译时提供这样的函数评估。
我收到以下错误:
在调用部分:
error: call to non-constexpr function ‘std::tuple<_Elements>& std::tuple<_Elements>::operator=(std::tuple<_Elements>&&) [with _Elements = {unsigned int, unsigned int, unsigned int}]’
在函数部分:
error: ‘constexpr std::array<std::tuple<unsigned int, unsigned int, unsigned int>, SIZE> GenArrayTuple() [with long unsigned int SIZE = 128]’ called in a constant expression
代码如下。
template<std::size_t SIZE>
constexpr std::array<std::tuple<uint32_t, uint32_t, uint32_t>, SIZE>
GenArrayTuple() {
std::array<std::tuple<uint32_t, uint32_t, uint32_t>, SIZE> array;
for (uint32_t i = 0; i < SIZE; ++i) {
// FIXME constexpr
arr[2*i] = std::make_tuple(i, i * 2, i * 3 + 1);
}
return array;
}
constexpr uint32_t n = 128;
constexpr auto array_tuple = GenArrayTuple<n>();
最佳答案
使用 std::make_tuple
实际上没有问题在 C++14 或更高版本的常量表达式中,因为 C++14 将其更改为 constexpr
。因此,只要用于初始化元组元素的任何类构造函数都计算为有效的常量表达式,它就是一个有效的常量表达式(当元素类型都是像 std::uint32_t
这样的标量时,就没有这样的构造函数)。
但请仔细查看错误消息。它提示的功能是(去掉一些细节)tuple& tuple::operator=(tuple&&)
。事实证明assignment operators的std::tuple
未标记constexpr
在当前的 C++ 版本中,意味着 tuple
的任何赋值object 不是有效的常量表达式。 (cppreference.com 指出,它们在 C++20 中将被标记为 constexpr
;这通常反射(reflect)了相应 C++ 工作组已接受的提案的更改。)
因此,要解决此问题,您需要初始化 array
一次全部完成,而不是在循环中分配其元素。也许最简单的方法是借助 std::make_integer_sequence
的帮助。 :
#include <tuple>
#include <array>
#include <cstdint>
#include <utility>
template <std::uint32_t ... I>
constexpr std::array<std::tuple<std::uint32_t, std::uint32_t, std::uint32_t>,
sizeof...(I)>
GenArrayTuple_helper(std::integer_sequence<std::uint32_t, I...>) {
return { std::make_tuple(I, I * 2, I * 3 + 1) ... };
}
template <std::size_t SIZE>
constexpr std::array<std::tuple<std::uint32_t, std::uint32_t, std::uint32_t>,
SIZE>
GenArrayTuple() {
return GenArrayTuple_helper(std::make_integer_sequence<std::uint32_t, SIZE>{});
}
关于c++ - 如何在编译时使用 std::make_tuple ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54702430/