gpt4 book ai didi

c++ - 带有 initializer_list 的编译时查找表

转载 作者:行者123 更新时间:2023-12-04 17:11:20 25 4
gpt4 key购买 nike

假设您有一些散列值,并希望在编译时将它们映射到各自的字符串。理想情况下,我希望能够按照以下方式写一些东西:

constexpr std::map<int, std::string> map = { {1, "1"}, {2 ,"2"} };

不幸的是,这在 C++17 和 C++2a 中都是不可能的。尽管如此,我尝试用 std::array 模拟它,但无法在编译时获取初始化列表的大小,从而在不显式指定大小的情况下正确设置数组类型。这是我的模型:


template<typename T0, typename T1>
struct cxpair
{
using first_type = T0;
using second_type = T1;

// interestingly, we can't just = default for some reason...
constexpr cxpair()
: first(), second()
{ }

constexpr cxpair(first_type&& first, second_type&& second)
: first(first), second(second)
{ }

// std::pair doesn't have these as constexpr
constexpr cxpair& operator=(cxpair<T0, T1>&& other)
{ first = other.first; second = other.second; return *this; }

constexpr cxpair& operator=(const cxpair<T0, T1>& other)
{ first = other.first; second = other.second; return *this; }

T0 first;
T1 second;
};

template<typename Key, typename Value, std::size_t Size = 2>
struct map
{
using key_type = Key;
using mapped_type = Value;
using value_type = cxpair<Key, Value>;

constexpr map(std::initializer_list<value_type> list)
: map(list.begin(), list.end())
{ }

template<typename Itr>
constexpr map(Itr begin, const Itr &end)
{
std::size_t size = 0;
while (begin != end) {
if (size >= Size) {
throw std::range_error("Index past end of internal data size");
} else {
auto& v = data[size++];
v = std::move(*begin);
}
++begin;
}
}

// ... useful utility methods omitted

private:
std::array<value_type, Size> data;
// for the utilities, it makes sense to also have a size member, omitted for brevity
};

现在,如果你只是用普通的 std::array 来做,事情就可以开箱即用:


constexpr std::array<cxpair<int, std::string_view>, 2> mapp = {{ {1, "1"}, {2, "2"} }};

// even with plain pair
constexpr std::array<std::pair<int, std::string_view>, 2> mapp = {{ {1, "1"}, {2, "2"} }};

不幸的是,我们必须明确地给出数组的大小作为第二个模板参数。这正是我想要避免的。为此,我尝试构建您在上面看到的 map 。有了这个伙伴,我们可以写一些东西,比如:

constexpr map<int, std::string_view> mapq = { {1, "1"} };
constexpr map<int, std::string_view> mapq = { {1, "1"}, {2, "2"} };

不幸的是,一旦我们超过 map 中神奇的 Size 常量,就会出现错误,因此我们需要明确给出大小:

//// I want this to work without additional shenanigans:
//constexpr map<int, std::string_view> mapq = { {1, "1"}, {2, "2"}, {3, "3"} };
constexpr map<int, std::string_view, 3> mapq = { {1, "1"}, {2, "2"}, {3, "3"} };

当然,一旦您在 constexpr 范围内throw,您就会得到一个编译错误并且可以显式地调整魔法常量。但是,这是我想隐藏的实现细节。用户不需要处理这些底层细节,这是编译器应该推断的内容。

不幸的是,我没有看到具有确切语法 map = { ... } 的解决方案。我什至看不到像 constexpr auto map = make_map({ ... }); 这样的东西。此外,这是与运行时内容不同的 API,我想避免使用它以提高易用性。

那么,是否有可能在编译时从初始化列表中推断出这个大小参数?

最佳答案

std::array 有推导指南:

template <class T, class... U>
array(T, U...) -> array<T, 1 + sizeof...(U)>;

它可以让你写:

// ok, a is array<int, 4>
constexpr std::array a = {1, 2, 3, 4};

我们可以遵循相同的原则,为map添加推导指南喜欢:

template <typename Key, typename Value, std::size_t Size>
struct map {
constexpr map(std::initializer_list<std::pair<Key const, Value>>) { }
};

template <class T, class... U>
map(T, U...) -> map<typename T::first_type, typename T::second_type, sizeof...(U)+1>;

允许:

// ok, m is map<int, int, 3>
constexpr map m = {std::pair{1, 1}, std::pair{1, 2}, std::pair{2, 3}};

不幸的是,这种方法需要在初始化列表中命名每个类型——你不能只写{1, 2}即使在你写了pair{1, 1}之后.


另一种方法是将右值数组作为参数:

template <typename Key, typename Value, std::size_t Size>
struct map {
constexpr map(std::pair<Key, Value>(&&)[Size]) { }
};

这避免了必须编写推导指南,让您只需在第一个上写类型,以额外的一对大括号或括号为代价:

// ok, n is map<int, int, 4>
constexpr map n{{std::pair{1, 1}, {1, 2}, {2, 3}, {3, 4}}};

// same
constexpr map n({std::pair{1, 1}, {1, 2}, {2, 3}, {3, 4}});

注意数组是pair<Key, Value>而不是 pair<Key const, Value> - 只允许写入 pair{1, 1} .由于您正在编写 constexpr map 无论如何,这种区别可能并不重要。

关于c++ - 带有 initializer_list 的编译时查找表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59155808/

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