C++ 如何在编译时接受任意长度的对列表?

标签 c++ templates

我希望构建一个编译时只读映射,并希望用 std::arraystd::tuple 支持它,其中每个元素是一个 std::pair。为了便于使用,我想避免在构造时对每个条目进行注释,并且我希望它能够推断出 map 中的元素数量,即:

constexpr MyMap<int, std::string_view> my_map{
  {1, "value1"},
  {2, "value2"},
};

我已经尝试了很多策略来做到这一点,但我似乎陷入了制作一个既能接受任意数量的元素又能告诉编译器所有大括号条目都被passed (e.x. {1, "value1"}) 是一对,否则无法推断出类型。

例如:

template <typename Key, typename Mapped, typename... Args>
constexpr auto make_map(std::pair<Key, Mapped>&& first, Args&&... args) {
  if constexpr (sizeof...(Args) == 0) {
    return std::tuple{std::forward<decltype(first)>(first)};
  }
  return std::tuple_cat(
    std::tuple{std::forward<decltype(first)>(first)},
    make_map(std::forward<Args>(args)...)
  );
}

似乎我可以制作一个宏,它可以让我快速制作函数的版本,比如所有参数都达到合理的数量(比如 10-15),但这感觉更丑陋和更糟。

有没有办法做我想做的事,或者我是否需要求助于宏或让用户用 std::pair 注释每个条目?

最佳答案

如果我没理解错的话, map 的大小是已知的并且是固定的?如果是这样,为什么不使用常规的 C 风格数组构造函数呢?不幸的是,没有办法让编译器推导直接初始化列表的类型(例如:推导 {1, "value"}std::pair<int, std::string_view> )所以,你必须指定推导的类型。

#include <array>
#include <string_view>
#include <utility>

template <typename K, typename V, size_t N>
class MyMap {
 public:
  using value_type = std::pair<K, V>;

  constexpr explicit MyMap(value_type(&&init)[N])
      : data_(std::to_array(std::forward<value_type[N]>(init))) {}

  const std::array<value_type, N> data_;
};

template <typename K, typename V, size_t N>
constexpr MyMap<K, V, N> MakeMyMap(
    typename MyMap<K, V, N>::value_type(&&init)[N]) {
  return MyMap{std::forward<typename MyMap<K, V, N>::value_type[N]>(init)};
}

int main(int argc, char* argv[]) {
  constexpr std::string_view value_1 = "value1";
  constexpr std::string_view value_2 = "value2";

  constexpr auto my_map = MakeMyMap<int, std::string_view>({
      {1, value_1},
      {2, value_2},
  });

  static_assert(my_map.data_.at(0) == std::make_pair(1, value_1));
  static_assert(my_map.data_.at(1) == std::make_pair(2, value_2));

  return EXIT_SUCCESS;
}

注意:这是c++20只因为 std::to_array (https://en.cppreference.com/w/cpp/container/array/to_array)。但是可以很容易地在 c++17 中实现它

#include <array>
#include <cstddef>
#include <type_traits>
#include <utility>

namespace internal {

template <bool Move = false, typename T, std::size_t... I>
constexpr std::array<std::remove_cv_t<T>, sizeof...(I)> to_array_impl(T (&a)[sizeof...(I)], std::index_sequence<I...>) {
  if constexpr (Move) {
    return {{std::move(a[I])...}};
  } else {
    return {{a[I]...}};
  }
}

}  // namespace internal

template <typename T, std::size_t N>
constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N]) noexcept(
    std::is_nothrow_constructible_v<T, T&>) {
  static_assert(!std::is_array_v<T>);
  static_assert(std::is_constructible_v<T, T&>);
  return internal::to_array_impl(a, std::make_index_sequence<N>{});
}

template <typename T, std::size_t N>
constexpr std::array<std::remove_cv_t<T>, N> to_array(T(&&a)[N]) noexcept(
    std::is_nothrow_move_constructible_v<T>) {
  static_assert(!std::is_array_v<T>);
  static_assert(std::is_move_constructible_v<T>);
  return internal::to_array_impl<true>(a, std::make_index_sequence<N>{});
}

关于C++ 如何在编译时接受任意长度的对列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70311461/

相关文章:

c++ - 请求转换为非标量类型

c++ - 使用boost将非平行边图保存到graphml文件

c++ - 在 C++ 中按值调用时存储变量拷贝的位置

c++ - 静态变量总是会耗尽内存吗?

c++ - 如何改善我的文件写入方法以减小Wavefront Object文件的大小?

带有 std::enable_if 的 C++ 可变参数模板部分模板特化

c++ - 没有从 'int' 到 'Student' 的可行转换

c++ - 使用模板模板参数进行参数推导

c++ - 当模板有默认参数时省略尖括号

c++ - 如何判断我的线程池何时完成其任务?