如果我们有以下条件:
template <class T>
struct B{
T data;
}
struct A{
int data_array[100];
}
int main()
{
A x;
const A x_const;
auto y1 = f(A());
auto y2 = f(x);
auto y3 = f(x_const);
auto y4 = f(std::move(x));
}我想知道一个f (最好是函数,但宏也可以),这样:
decltype(y1) == B<A>
decltype(y2) == B<A&>
decltype(y3) == B<const A&>
decltype(y4) == B<A&&>也就是说,f完美地将x转发到B的对象中。
发布于 2011-06-09 10:05:11
这是不可能的。对于y1和y4,它们都采用类型A的右值,但您希望它们返回不同的类型。f如何知道要返回什么?
发布于 2011-06-09 09:58:45
template <typename T>
auto f(T&& t) -> B<decltype(std::forward<T>(t))>
{
return B<decltype(std::forward<T>(t))>{std::forward<T>(t)};
}这样做的几乎你想要的。唯一的区别是第一个类型是B<A&&>而不是B<A>。
发布于 2011-06-09 10:07:37
auto y1 = f(A());
auto y4 = f(std::move(x));将无法区分,因为A()会生成一个绑定到A&&的临时文件。
https://stackoverflow.com/questions/6287221
复制相似问题