c++ - 如何在编译时使用 std::make_tuple ?

标签 c++ c++14 c++17 std constexpr

返回 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 operatorsstd::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/

相关文章:

c++ - 前进或 move 与否;如何确定在类的使用上下文中哪个是首选?

c++ - 强类型定义

php - 将 php 嵌入到 C++ 应用程序中或以任何方式完成?

c++ - 如何使多个模板类具有相同的类型

c++ - 使用动态矩阵进行特征分解时出现错误

c++ - 为什么选择转换运算符的这种重载?

c++ - 我可以在 using 声明中正确使用 C++20 概念吗?

c++ - template<class = enable_if_t<...>> 做什么?

c++ - 如何在编译时将常量 float 值转换为 4 字节十六进制常量值?

C++ 转换运算符到 chrono::duration - 适用于 c++17 但不适用于 C++14 或更低版本