R ++在C ++ 03 [英] Rvalues in C++03

查看:170
本文介绍了R ++在C ++ 03的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何判断给定参数是否为C ++ 03中的右值?我写了一些非常通用的代码,如果可能的话需要参考,否则构造一个新的对象。我可以重载以取值以及by-reference和rvalue返回调用by-value函数?



或者我有一个非常恶心的感觉这是为什么右值引用是在C ++ 0x?



编辑:



is_rvalue =!显然有一种方法可以确定一个表达式是否为C ++ 03中的右值或左值(如果是这样的话):

我说明显是因为我不知道我是多么理解的技术)。注意,为了使该技术可用,预处理器宏是非常需要的。 Eric Niebler撰写了一篇关于它如何工作以及如何在BOOST_FOREACH中使用的文章:





注意这篇文章是相当沉重的阅读(至少是对我来说);正如Neibler所说:


毫无疑问,这是一个神秘的东西,但我们得到一个鲁棒的方法来检测rvalue-


使用artcile中描述的rvalue检测可能会帮助您处理任何表达式的至少一些C ++ 0x的右值引用解析的问题。


How can you tell whether or not a given parameter is an rvalue in C++03? I'm writing some very generic code and am in need of taking a reference if possible, or constructing a new object otherwise. Can I overload to take by-value as well as by-reference and have the rvalue returns call the by-value function?

Or do I have a very sickening feeling that this is why rvalue references are in C++0x?

Edit:

is_rvalue = !(is_reference || is_pointer) ?

解决方案

There apparently is a way to determine whether an expression is an rvalue or lvalue in C++03 (I say apparently because I'm not sure how well I understand the technique). Note that to make the technique usable, preprocessor macros are pretty much required. Eric Niebler has written a nice article about how it works and how it gets used in BOOST_FOREACH:

Note that the article is pretty heavy reading (at least it is for me); as Neibler says in it:

There's no doubt that this is arcane stuff, but we are rewarded with a robust way to detect the rvalue-ness and lvalue-ness of any expression.

Using the rvalue detection described in the artcile might help you deal with at least some of the issues that C++0x's rvalue references solve.

这篇关于R ++在C ++ 03的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆