C ++中的R值到底是什么? [英] What exactly is an R-Value in C++?

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

问题描述

有人可以解释一下R值是什么吗?我不太确定这是什么,我的项目必须将其合并。这是R值的说明(第一部分是r_string.hpp):

Could someone explain, or point me to some sort of explanation, of what R-Value is? I'm not really sure what it is, and my project has to incorporate it. Here is a demonstration of what R-Value is(First part is the r_string.hpp):

#include <algorithm>
#include <iostream>

template <typename CHAR_T = char>

class basic_rstring {
public:
    typedef CHAR_T  value_type;
    typedef CHAR_T* pointer_type;
    typedef CHAR_T const*   pointer_const_type;
private:
    pointer_type    _data;
    std::size_t     _length;
public:
    basic_rstring() : _data(nullptr), _length(0) 
    {
        std::cout << "Default ctor\n";
    }

    basic_rstring( pointer_const_type s ) 
        : _data( nullptr )
        , _length( 0 )
    {
        std::cout << "Literal ctor: " << s << std::endl;
        _length = strlen( s );
        _data = new value_type[ _length + 1 ];
        std::copy( s, s + _length + 1, _data );
    }

    basic_rstring( basic_rstring const& s )     
        : _data( nullptr )
        , _length( s._length )
    {
        std::cout << "Copy ctor: " << s.c_str() << std::endl;
        _data = new value_type [ _length + 1 ];
        std::copy( s._data, s._data + s._length + 1, _data );
    }

    basic_rstring( basic_rstring && s )     //move constructor
        : _data( s._data )
        , _length( s._length )
    {
        std::cout << "Move ctor: " << s.c_str() << std::endl;
        s._data = nullptr;
        s._length = 0;
    }

    ~basic_rstring()
    {
        if( _data )
            std::cout << "dtor: " << _data << "\n";
        else 
            std::cout << "NULL dtor\n";
        delete [] _data;
    }

    basic_rstring& operator = ( basic_rstring const& s );
    basic_rstring& operator = ( basic_rstring && s )
    {
        std::cout << "RValue assignment: " << s.c_str();
        if( _data )
            std::cout << " deleting...." << std::endl;
        else 
            std::cout << " no delete..." << std::endl;
        delete [] _data;
        _data = s._data;
        s._data = nullptr;
        _length = s._length;
        s._length = 0;
        return *this;
    }

    pointer_const_type c_str() const { return _data; }

};

template <typename CHAR_T>
basic_rstring<CHAR_T>& basic_rstring<CHAR_T>::operator = ( basic_rstring const& s )
{
    std::cout << "Copy assignment: " << s.c_str() << std::endl;
    pointer_type newData = new value_type [ s._length + 1 ];
    std::copy( s._data, s._data + s._length + 1, newData );
    _length = s._length;
    delete [] _data;
    _data = newData;
    return *this;
}

typedef basic_rstring<char> String;
typedef basic_rstring<wchar_t> wString;


#define _SCL_SECURE_NO_WARNINGS
#include "Rstring.hpp"
using namespace std;
#define BOOST_TEST_MODULE move_test
#include <boost/test/unit_test.hpp>

template <typename T_>
void old_swap( T_& a, T_&b ) 
{
    T_ hold = a;
    a = b;
    b = hold;
}

BOOST_AUTO_TEST_CASE( stuff )
{
    String s("Bert");
    String t("Ernie");
    cout << "Old swap" << endl;
    old_swap(s,t);
    BOOST_CHECK( !strcmp( "Bert", t.c_str() ) );
    BOOST_CHECK( !strcmp( "Ernie", s.c_str() ) );

    cout << "New swap" << endl;
    swap(s,t);
    BOOST_CHECK( !strcmp( "Bert", s.c_str() ) );
    BOOST_CHECK( !strcmp( "Ernie", t.c_str() ) );

    cout << "\nDone." << endl;

}


推荐答案


有人可以解释一下R值是什么,还是让我指向某种解释?我不太确定它是什么。

"Could someone explain, or point me to some sort of explanation, of what R-Value is? I'm not really sure what it is"

术语左值最初是指一个表达式,该表达式可以是赋值的 left 手。相应地,一个 rvalue (尽管我记得C89标准没有使用该术语),本来是相反的:一个表达式不能是赋值的左侧,但是可以

The term lvalue originally referred to an expression that could be the left hand side of an assignment. Correspondingly, an rvalue (although as I recall that term was not used by the C89 standard), was originally just the opposite: an expression that could not be the left hand side of an assignment, but that could only be the right hand side.

C ++ 11通过添加更多细微差别的术语使这一点复杂化,但让我们专注于C ++ 03的含义。

C++11 complicated this by adding several more nuanced terms, but let's concentrate on the C++03 meanings.

例如,如果您拥有

int x;

然后赋值 x = 42 是确定的,因此 x 是左值表达式。

then the assignment x = 42 is OK, so x is an lvalue expression.

作为反例,该配属 x + 0 = 42 不好,所以 x + 0 是一个右值表达式。

As a counter-example, the assigment x+0 = 42 is not OK, so x+0 is an rvalue expression.

2 + 2 表达式也是一个右值表达式。

And so is the expression 2+2, it's an rvalue expression.

因此,如果需要是您的程序应包含一个右值,然后只需编写 2 + 2 或例如(更高级) 6 * 7 ,在 main 中。

So, if the requirement is that your program should include an rvalue, then just write 2+2 or e.g. (more advanced) 6*7, in main.

原始C没有 const 。在C ++中,使用 const ,您必须忽略 const 以便将表达式指定为左值或右值。那么关键的一点是保证的表达式是否指向内存中的对象,即具有地址的对象:如果是,则表达式为左值。

Original C didn't have const. In C++, with const, you have to disregard the const for the purpose of designating an expression as lvalue or rvalue. The critical point is then whether the expression guaranteed refers to an object in memory, an object with an address: if so, then the expression is an lvalue.

引用类型表示左值,因为引用类型的表达式必须引用具有内存地址的对象,即该表达式是左值。

A reference type implies lvalue, because an expression of reference type is necessarily referring to an object with a memory address, i.e. that expression is an lvalue.

但是,除引用外,没有任何连接在类型和左值/右值之间。例如, x x + 0 都是 int ,它们产生相同的 int 值。但是前者是左值表达式,而后者是右值表达式。

However, other than references there's no connection between type and lvalue/rvalue. For example, both x and x+0 are expressions of type int, and they yield the same int value. But the former is an lvalue expression, while the latter is an rvalue expression.

通常,如果您可以应用内置地址运算符,则它是一个左值表达式,否则为右值表达式。

As a general rule, if you can apply the built-in address operator, then it's an lvalue expression, and otherwise it's an rvalue expression.

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

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