C++03 中的右值

发布于 2024-09-05 07:07:19 字数 206 浏览 2 评论 0原文

在 C++03 中如何判断给定参数是否为右值?我正在编写一些非常通用的代码,如果可能的话需要引用,否则需要构造一个新对象。我可以重载以按值和按引用并让右值返回调用按值函数吗?

或者我是否有一种非常恶心的感觉,这就是为什么右值引用出现在 C++0x 中?

编辑:

is_rvalue = !(is_reference || is_pointer) ?

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) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

羁拥 2024-09-12 07:07:19

显然有一种方法可以确定 C++03 中的表达式是右值还是左值(我说显然是因为我不确定我对这项技术的理解程度)。请注意,为了使该技术可用,几乎需要预处理器宏。 Eric Niebler 写了一篇很好的文章,介绍了它的工作原理以及如何在 BOOST_FOREACH 中使用它:

请注意,这篇文章的阅读量相当大(至少对我来说);正如内布尔勒在其中所说:

毫无疑问,这是一个神秘的东西,但我们获得了一种强大的方法来检测任何表达式的右值和左值。

使用本文中描述的右值检测可能会帮助您至少处理 C++0x 的右值引用解决的一些问题。

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.

黄昏下泛黄的笔记 2024-09-12 07:07:19

如何判断给定参数是否为 C++03 中的右值?

你不能。你所能做的就是相信你的客户并欺骗:

void call_me_with_anything(const T& const_ref)
{
    /* read from const_ref */
}

void call_me_with_rvalue_only_please_i_trust_you(const T& const_ref)
{
    T& ref = const_cast<T&>(const_ref);
    /* write through ref */
}

我正在编写一些非常通用的代码

那么恐怕 C++03 无法满足您的需求。

How can you tell whether or not a given parameter is an rvalue in C++03?

You can't. All you can do is trust your clients and cheat:

void call_me_with_anything(const T& const_ref)
{
    /* read from const_ref */
}

void call_me_with_rvalue_only_please_i_trust_you(const T& const_ref)
{
    T& ref = const_cast<T&>(const_ref);
    /* write through ref */
}

I'm writing some very generic code

Then I'm afraid C++03 won't cut it for you.

天暗了我发光 2024-09-12 07:07:19

或者我是否有一种非常恶心的感觉,这就是 C++0x 中存在右值引用的原因?

你是对的,你需要 C++0x 和右值引用来做到这一点。

我能想到的最接近的事情是通过常量引用获取某些内容,因为它可以绑定到左值或右值(如果右值需要构造一个临时值,它会这样做)。也就是说,您的代码将无法区分两者。

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

You are correct, you need C++0x and rvalue references to do that.

The closest thing I can think of is to take something by const-reference as that can bind to either an lvalue or an rvalue (if the rvalue needs to construct a temporary it will). That said, your code will not be able to distinguish between the two.

没有心的人 2024-09-12 07:07:19

或者我是否有一种非常恶心的感觉,这就是 C++0x 中存在右值引用的原因?

是的。这正是它们被添加到 C++0x 中的原因。
您无法在 C++ 中创建重载来区分右值和左值。

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

Yup. That is exactly why they're added to C++0x.
You can't create overloads in C++ to distinguish between rvalues and lvalues.

冬天旳寂寞 2024-09-12 07:07:19

我正在编写一些非常通用的代码,并且需要在可能的情况下获取引用,否则需要构造一个新对象。

这不是您想要的吗?

void f(T& t);
void f(T const& t);

注意...

T t; f(t); // calls f(T&)
f(T()); // calls f(T const&)

I'm writing some very generic code and am in need of taking a reference if possible, or constructing a new object otherwise.

Won't this do what you want?

void f(T& t);
void f(T const& t);

Note that...

T t; f(t); // calls f(T&)
f(T()); // calls f(T const&)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文