我有一个例子来说明我的问题:
#include <utility>
class Foo {
public:
Foo(int i) {}
};
template<typename T, typename ...Args>
class Bar {
public:
T t;
Bar(Args &&... args) : t(std::forward<Args>(args)...) {}
};
Run Code Online (Sandbox Code Playgroud)
如果我想实例化这个模板:
Bar<Foo> foo(1);
Run Code Online (Sandbox Code Playgroud)
编译器抛出一个错误:
no matching function for call to ‘Bar<Foo>::Bar(int)’
Run Code Online (Sandbox Code Playgroud)
所以我必须写信给这个:
Bar<Foo, int> foo(1);
Run Code Online (Sandbox Code Playgroud)
这很烦人,尤其是当我得到一些包含一长串参数的类时。
那么有什么方法可以摆脱在参数包中显式显示类型
如果你想构造转发,使该模板
template<typename T>
class Bar {
public:
T t;
template<typename ...Args>
Bar(Args &&... args) : t(std::forward<Args>(args)...) {}
};
Run Code Online (Sandbox Code Playgroud)
t无论如何,我们通常只关心在 的初始化期间参数的类型。
| 归档时间: |
|
| 查看次数: |
70 次 |
| 最近记录: |