相关疑难解决方法(0)

什么是右值,左值,x值,glvalues和prvalues?

在C++ 03中,表达式是rvaluelvalue.

在C++ 11中,表达式可以是:

  1. 右值
  2. 左值
  3. x值
  4. glvalue
  5. prvalue

两类已成为五大类.

  • 这些新的表达类别是什么?
  • 这些新类别如何与现有的左值和左值类别相关联?
  • C++ 0x中的右值和左值类别是否与它们在C++ 03中的相同?
  • 为什么需要这些新类别?是WG21神只是想迷惑我们凡人?

c++ expression c++-faq c++11

1291
推荐指数
13
解决办法
17万
查看次数

clang 中奇怪的右值引用

下面的代码:

#include <iostream>

struct A {
  A() { std::cout << "()" << std::endl; }  
  A(A&&) { std::cout << "(A&&)" << std::endl; }  
  A(const A&) { std::cout << "(const A&)" << std::endl; }  
};

A fun (A&& a){
  return a;
}

int main(){
  A a;
  fun(std::move(a));
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

在 clang 16.0.3 (arm64-apple-darwin22.4.0 和 c++17) 中产生

()
(A&&)
Run Code Online (Sandbox Code Playgroud)

虽然大多数编译器给出

()
(const A&)
Run Code Online (Sandbox Code Playgroud)

哪一个是正确的?

c++ clang rvalue-reference move-semantics c++17

6
推荐指数
1
解决办法
134
查看次数

复制ctor调用而不是移动ctor

为什么在从而bar不是移动构造函数返回时调用复制构造函数?

#include <iostream>

using namespace std;

class Alpha {
public:
  Alpha() { cout << "ctor" << endl; }
  Alpha(Alpha &) { cout << "copy ctor" << endl; }
  Alpha(Alpha &&) { cout << "move ctor" << endl; }
  Alpha &operator=(Alpha &) { cout << "copy asgn op" << endl; }
  Alpha &operator=(Alpha &&) { cout << "move asgn op" << endl; }
};

Alpha foo(Alpha a) {
  return a; // Move ctor is called (expected).
}

Alpha bar(Alpha …
Run Code Online (Sandbox Code Playgroud)

c++ constructor copy-constructor move-constructor c++11

5
推荐指数
1
解决办法
180
查看次数