use*_*764 3 c++ variadic-templates c++11
我有一个对象'S'存储一个简单的指针元组,通过使用可变参数模板使其变得灵活.有两种方法,store()和store2().第一个(商店)工作正常.第二个将无法编译,因为std :: make_tuple失败并显示错误:
'调用'make_tuple'没有匹配函数
它进一步补充说,第一个参数没有从'B*'到'B*&&'的已知对话(这个错误在元组库头中很深).
代码在这里:
#include <tuple>
#include <utility>
template<typename...Rs>
struct S
{
void store(std::tuple<Rs*...> rs)
{
rs_ = rs;
}
void store2(Rs*...rs)
{
rs_ = std::make_tuple<Rs*...>(rs...); // This is the guy that breaks
}
private:
std::tuple<Rs*...> rs_;
};
struct B
{};
struct A : S<B, B>
{};
int main()
{
auto *b1 = new B;
auto *b2 = new B;
auto *a1 = new A;
a1->store(std::make_tuple(b1, b2)); // This works
a1->store2(b1, b2); // How can I get this to work?
// (It causes an error in std::make_tuple of store2 above)
}
Run Code Online (Sandbox Code Playgroud)
这是一个错误,因为make_tuple像C++ 11或更高版本一样make_pair,它会转发引用,当您明确指定非引用的模板参数时,这些转发引用将成为右值引用.
所以make_tuple<Rs*...>就是tuple<Rs*...> make_tuple(Rs*&&...)-参数类型都是右值引用,不绑定到左值(和rs...扩展到左值的列表).
这些make_meow函数的重点是避免写出显式模板参数,所以......不要写它们.
| 归档时间: |
|
| 查看次数: |
1865 次 |
| 最近记录: |