c++ - 可变参数模板构造中的隐式 std::pair 构造

标签 c++ templates c++11 c++14 variadic-templates

我有一个 constexpr 键值映射,大致具有以下定义:

// map with `pos` remaining entries
template<typename K, typename V, size_t pos>
class Map {
public:
    template<class Head, class... Tail>
    constexpr Map(Head head, Tail... tail)
        :
        value{head},
        tail{tail...} {}

    Element<K, V> value;
    const Map<K, V, pos - 1> tail;

    // members etc
};

// map end element.
template<typename K, typename V>
class Map<K, V, 0> {
public:
    constexpr Map() {}

    // end element specifics.
};

为了在编译时初始化键值映射,我有一个转发元素的实用函数:

template<typename K, typename V, typename... Entries>
constexpr Map<K, V, sizeof...(Entries)> create_const_map(Entries&&... entry) {
    return Map<K, V, sizeof...(entry)>(Entry<K, V>{std::forward<Entries>(entry)}...);
}

元素定义为:

template<class K, class V>
class Entry {
public:
    constexpr Entry(const K &key, const V &value)
        :
        key{key},
        value{value} {}

    constexpr Entry(std::pair<K, V> pair)
        :
        key{pair.first},
        value{pair.second} {}

    const K key;
    const V value;
};

要实际创建 map ,我可以成功使用 std::make_pair:

constexpr auto cmap = create_const_map<int, int>(
    std::make_pair(0, 0),
    std::make_pair(13, 37),
    std::make_pair(42, 9001)
);

我现在想要的是消除对 std::make_pair 的调用并改用大括号:

constexpr auto cmap = create_const_map<int, int>(
    {0, 0},
    {13, 37},
    {42, 9001}
);

但这会导致编译失败,因为 {} 未被推断为对的构造:

/home/jj/devel/openage/libopenage/datastructure/tests.cpp:191:24: error: no matching function for call to 'create_const_map'
        constexpr auto cmap = create_const_map<int, int>(
                              ^~~~~~~~~~~~~~~~~~~~~~~~~~
/home/jj/devel/openage/libopenage/datastructure/constexpr_map.h:286:46: note: candidate function not viable: requires 0 arguments, but 3 were provided
constexpr Map<K, V, sizeof...(Entries)> create_const_map(Entries&&... entry) {
                                        ^

我该怎么做才能使用 {a, b} 而不是 std::make_pair(a, b)

最佳答案

这不容易实现,因为正如@Rostislav 所说,如果参数是大括号初始化列表,则编译器无法为其推断类型。

我通过手动创建足够多的重载函数来解决这个问题。在下文中,重载是函数调用运算符 (operator()),而 create_map 是该函数对象的变量模板。您将要创建的重载数传递给 Overload。我的 C++ 很粗糙,所以这也许并不理想,但它似乎可行。

template<typename D, int N, typename ...E>
struct OverloadImpl;

template<typename D, int N, typename E, typename ...Es>
struct OverloadImpl<D, N, E, Es...> : OverloadImpl<D, N-1, E, E, Es...> {
    decltype(auto) operator()(const E& e, const Es&... es) {
       return static_cast<D&>(*this).exec(e, es...);
    }
    using OverloadImpl<D, N-1, E, E, Es...>::operator();
};

template<typename D, typename E, typename ...Es>
struct OverloadImpl<D, 0, E, Es...> { 
    decltype(auto) operator()() { 
       return static_cast<D&>(*this).exec();   
    }    
};

template<typename D, typename E, int N>
struct Overload : OverloadImpl<D, N, E>
{ };

要将以上内容应用于您的案例,您可以从 Overload 派生并提供一个 exec 函数。

template<typename K, typename V, int C>
struct M {
   template<typename ...T>
   M(T&&...) { }
};

template<typename K, typename V>
struct Entry {
   Entry(K, V) { }    
};

template<typename K, typename V>
struct CreateMapImpl : Overload<CreateMapImpl<K, V>, Entry<K, V>, 10> {
    template<typename ...T>
    M<K, V, sizeof...(T)> exec(const T&... t) {
       return { t... };   
    }
};

template<typename K, typename V>
CreateMapImpl<K, V> create_map{};

int main() {
   create_map<int, std::string>({1, "one"}, {2, "two"}, {3, "three"});   
}

关于c++ - 可变参数模板构造中的隐式 std::pair 构造,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32669834/

相关文章:

c++ - 可变参数模板类 : Infer types based on constructor parameters

c++ - 让 std::ifstream 处理 LF、CR 和 CRLF?

c++ - 海湾合作委员会的错误?关于模板类中友元函数的访问控制问题

c++ - 如何将 block 压缩行转换为密集矩阵?

C++ 模板运算符重载不同类型,自动返回类型

c++ - 使用 Eigen header 的模板函数

c++ - 将闭包作为“类方法指针”?

c++ - 了解 std::regex 声明:运行时的 regex_error 异常

linux - gdb demangler 加载符号时出现段错误

c++ - 如何读取输入流缓冲区中存储的字符数