右值转换Visual Studio [英] rvalue to lvalue conversion Visual Studio

查看:79
本文介绍了右值转换Visual Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2012RC中有一些非标准扩展。例如,此代码编译:

In Visual Studio 2012RC there are some non-standard extensions. For example this code compiles:

#include <string>

using namespace std;

void value(string& value)
{
    value = "some";
}

int main()
{
    value(string("nice"));
}

并且警告它是非标准扩展。所以,我想了解,它是真实的,如何代码转换(rvalue引用或const引用const_cast)?

and get warning that it is non-standard extension. So, I want to understand, how it's real and how code transforms (rvalue-reference or const reference with const_cast)?

推荐答案

类类型的临时对象仍然是对象。它存在于内存中,这意味着在编译器中没有什么不寻常的可以附加一个引用。在物理层,无论是const引用还是非const引用都没有区别。换句话说,在语言限制纯粹是概念性的情况下,人为的。编译器只是忽略了这个限制。没有必要在这里转换任何东西。引用简单地直接附加到对象,无论该对象碰巧驻留在哪里。

A temporary object of class type is still an object. It lives somewhere in memory, which means that there's nothing unusual in the compiler being able to attach a reference to it. At physical level whether it is a const reference or non-const reference makes no difference. In other words, in cases like that the language restriction is purely conceptual, artificial. The compiler simply ignores that restriction. There's no need to "transform" anything here. The reference is simply attached directly to the object, wherever that object happens to reside.

基本上,对于提供外部字访问其这个指针lvalue访问 * this )可以立即轻松地模拟行为。

Basically, for a class that provides the outside word with access to the value of its this pointer (or with lvalue access to *this) the behavior can be immediately and easily simulated

struct S {
  S& get_lvalue() { return *this; }
};

void foo(S& s);
...

foo(S().get_lvalue());

上述代码是完全合法的,它适用于上述限制。你可以认为MSVC ++的行为与此相当。

The above code is perfectly legal and it works around the aforementioned restriction. You can think of MSVC++ behavior as being equivalent to this.

这篇关于右值转换Visual Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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