相关疑难解决方法(0)

变量模板+ std :: map的通用lambdas

一个答案C++ 14个可变模板:目的是什么?任何用法示例?提出了一个变量模板+泛型lambdas的用法示例,它看起来像这样:

void some_func() {
    template<typename T>
    std::map<int, T> storage;

    auto store = []<typename T>(int key, const T& value) { storage<T>.insert(key, value) };

    store(0, 2);
    store(1, "Hello"s);
    store(2, 0.7);

    // All three values are stored in a different map, according to their type. 
}
Run Code Online (Sandbox Code Playgroud)

不幸的是它没有编译所以我试图"修复"它,这是我到目前为止的尝试.

#include <map>

template<typename T>
std::map<int, T> storage;

void some_func() {

    auto store = [](int key, const auto& value) { storage<decltype(value)>.insert(key, value); };

    store(0, 2);
    store(1, std::string("Hello"));
    store(2, 0.7);
}
Run Code Online (Sandbox Code Playgroud)

错误消息是:

main.cpp:7:76: error: no …
Run Code Online (Sandbox Code Playgroud)

c++ c++14

5
推荐指数
1
解决办法
1213
查看次数

标签 统计

c++ ×1

c++14 ×1