c11 ravlue test example 1

c11 ravlue test example 1
vs2010


#include <string>
#include <iostream>
using namespace std;

string modifiable_rvalue() {
    return "cute";
}

const string const_rvalue() {
    return "fluffy";
}


void teststring(string && r)
{
    cout<<"test rvalue string"<<"\n";
}


void teststring(string & r)
{
    cout<<"test lvalue string"<<"\n";
}


int main() {
    string modifiable_lvalue("kittens");
    const string const_lvalue("hungry hungry zombies");

    string& a = modifiable_lvalue;          // Line 16
    //      string& b = const_lvalue;               // Line 17 - ERROR
    //      string& c = modifiable_rvalue();        // Line 18 - ERROR
    //      string& d = const_rvalue();             // Line 19 - ERROR

    const string& e = modifiable_lvalue;    // Line 21
    const string& f = const_lvalue;         // Line 22
    const string& g = modifiable_rvalue();  // Line 23
    const string& h = const_rvalue();       // Line 24

    //string&& i = modifiable_lvalue;         // Line 26 - ERROR
    //string&& j = const_lvalue;              // Line 27 - ERROR
    string&& k = modifiable_rvalue();       // Line 28
   //string&& l = const_rvalue();            // Line 29 - ERROR

    //const string&& m = modifiable_lvalue;   // Line 31 - ERROR
    //const string&& n = const_lvalue;        // Line 32 - ERROR
    const string&& o = modifiable_rvalue(); // Line 33
    const string&& p = const_rvalue();      // Line 34


    teststring(string("1234"));

    string s = "1234";
    teststring(s);

 

}



                                   左值可变        左值const                      右值可变                  右值const
左值引用                          O                  X                                X                            X
左值const引用                  O                  O                                O                            O

右值引用                          X                  X                                O                            X     
 右值const引用                 X                  X                                O                            O






你可能感兴趣的:(c11 ravlue test example 1)