【发布时间】:2015-07-03 00:34:55
【问题描述】:
是否有可能有一个 struct A 本身带有一个移动构造函数和一些可以从其他类型移动的构造函数(例如 struct B )但是有一个模板推导正在进行,这样类型 B 不是直接硬编码为另一个移动构造函数:
struct A{
A()= default;
A(A&&a){ /* A's move constructor */ }
template<typename T>
A(T&&t){
/* (not a move constructor! by std., matches also lvalues)
move from t
(meta programming to check if we can move the type)
*/
}
}
struct B{};
上面的问题是
B b;
A a(std::move(b)); // select the templated constructor (which moves)
A a(b); // selects the same copy constructor (which moves but we do not want!!
如何做到这一点?
【问题讨论】:
-
注意
T&&t是一个转发引用,它允许T推断引用类型(不像A(B&& b)`)。所以这个构造函数实际上也会匹配左值。 -
这就是我有如何区分这一点的确切问题,但要继续进行模板推导过程......?
-
可能有一些技巧,但我想不出任何东西。这将是另一个问题的主题!
-
Argggghhhh 你的问题变了。 FFS。
-
@Gabriel 我强烈建议回滚到原来的问题并单独发布新问题,你所做的只是烦人
标签: c++ templates c++11 c++14 move-semantics