将临时值作为 LValue 传递
我想使用以下习语,我认为这是非标准的。我有利用返回值优化返回向量的函数:
vector<T> some_func()
{
...
return vector<T>( /* something */ );
}
然后,我想使用,
vector<T>& some_reference;
std::swap(some_reference, some_func());
但 some_func
不返回 LValue。上面的代码很有道理,我发现这个习惯用法非常有用。然而,它是非标准的。 VC8 仅发出最高警告级别的警告,但我怀疑其他编译器可能会拒绝它。
我的问题是:是否有某种方法可以实现我想做的同样的事情(即构造一个向量,分配给另一个向量,然后销毁旧的向量),这是合规的(并且不使用赋值运算符,见下文)?
对于我编写的类,我通常会
class T
{
T(T const&);
void swap(T&);
T& operator=(T x) { this->swap(x); return *this; }
};
利用复制省略来实现分配,并解决我的问题。然而,对于标准类型,我真的很想使用 swap
因为我不想要临时的无用副本。
由于我必须使用 VC8 并生成标准 C++,因此我不想听到有关 C++0x 及其右值引用的信息。
编辑:最后,当我使用左值时,我想到了这一点
typedef <typename T>
void assign(T &x, T y)
{
std::swap(x, y);
}
,因为如果 y 是临时的,编译器可以自由地优化对复制构造函数的调用,并且当我有左值时,可以使用 std::swap 。我使用的所有类都是实现非愚蠢版本的 std::swap 所“必需”的。
I'd like to use the following idiom, that I think is non-standard. I have functions which return vectors taking advantage of Return Value Optimization:
vector<T> some_func()
{
...
return vector<T>( /* something */ );
}
Then, I would like to use
vector<T>& some_reference;
std::swap(some_reference, some_func());
but some_func
doesn't return a LValue. The above code makes sense, and I found this idiom very useful. However, it is non-standard. VC8 only emits a warning at the highest warning level, but I suspect other compilers may reject it.
My question is: Is there some way to achieve the very same thing I want to do (ie. construct a vector, assign to another, and destroy the old one) which is compliant (and does not use the assignment operator, see below) ?
For classes I write, I usually implement assignment as
class T
{
T(T const&);
void swap(T&);
T& operator=(T x) { this->swap(x); return *this; }
};
which takes advantage of copy elision, and solves my problem. For standard types however, I really would like to use swap
since I don't want an useless copy of the temporary.
And since I must use VC8 and produce standard C++, I don't want to hear about C++0x and its rvalue references.
EDIT: Finally, I came up with
typedef <typename T>
void assign(T &x, T y)
{
std::swap(x, y);
}
when I use lvalues, since the compiler is free to optimize the call to the copy constructor if y is temporary, and go with std::swap
when I have lvalues. All the classes I use are "required" to implement a non-stupid version of std::swap
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于 std::vector 是类类型,并且可以在右值上调用成员函数:
Since
std::vector
is a class type and member functions can be called on rvalues:如果您不想要无用的临时副本,请不要按值返回。
使用(共享)指针,通过引用传递要填充的函数参数,插入迭代器,...
您想按值返回有什么具体原因吗?
If you don't want useless copies of temporaries, don't return by value.
Use (shared) pointers, pass function arguments by reference to be filled in, insert iterators, ....
Is there a specific reason why you want to return by value?
我知道在标准的限制内实现您想要的唯一方法是应用表达式模板元编程技术:http://en.wikipedia.org/wiki/Expression_templates 对于您的情况来说这可能很容易,也可能不容易。
The only way I know - within the constraints of the standard - to achieve what you want are to apply the expression templates metaprogramming technique: http://en.wikipedia.org/wiki/Expression_templates Which might or not be easy in your case.