Ole*_*leg 27 c++ templates c++11
通过一个例子来解释我的意思可能更容易.想象一下以下模板:
template <class... Args>
std::tuple<Args...> foo();
Run Code Online (Sandbox Code Playgroud)
例如,可以调用它,如下所示:
auto ret = foo<int, bool>();
Run Code Online (Sandbox Code Playgroud)
但是如果我想根据可变参数模板参数的数量将其他参数传递给函数呢?例如,假设我想为每个Args传递一个字符串文字:
auto ret = foo<int, bool>("a", "b");
Run Code Online (Sandbox Code Playgroud)
这个问题是,似乎不可能扩展非可变参数,所以下面显然不能编译:
template <class... Args>
std::tuple<Args...> foo(const char*... names);
Run Code Online (Sandbox Code Playgroud)
有没有明智的方法来实现这个?
Dan*_* M. 45
你可以用类似的东西做到这一点
template <class... Args>
std::tuple<Args...> foo(proxy<Args, const char*>... names);
Run Code Online (Sandbox Code Playgroud)
这里proxy是
template<class T, class E>
using proxy = E;
Run Code Online (Sandbox Code Playgroud)
你可以在这里看到这个:https://godbolt.org/g/SHBYzy
| 归档时间: |
|
| 查看次数: |
1180 次 |
| 最近记录: |