相关疑难解决方法(0)

decltype和括号

我不明白FCD第148页上的例子的最后一行(§7.6.1.2/ 4):

const int&& foo();
int i;
struct A { double x; };
const A* a = new A();
decltype(foo()) x1 = i;     // type is const int&&
decltype(i) x2;             // type is int
decltype(a->x) x3;          // type is double
decltype((a->x)) x4 = x3;   // type is const double&
Run Code Online (Sandbox Code Playgroud)

为什么括号在这里有所作为?它不应该只是double像上面那样吗?

c++ type-inference decltype c++11

50
推荐指数
3
解决办法
3617
查看次数

在C++中,哪些类别(左值,右值,右值等)可以生成类类型临时的表达式?

这是一些示例代码:

#include <iostream>

class Foo
{
public:
  explicit Foo(int x) : data(x) {};

  Foo& operator++()
  {
    data += 1;
    return *this;
  }

  void *get_addr()
  {
    return (void*)this;
  }

  friend Foo operator + (const Foo& lhs, const Foo& rhs);
  friend std::ostream& operator << (std::ostream& os, const Foo& f);

private:
  int data;
};

std::ostream& operator << (std::ostream& os, const Foo& f)
{
  return (os << f.data);
}

Foo operator + (const Foo& lhs, const Foo& rhs)
{
  return Foo(lhs.data + rhs.data);
} …
Run Code Online (Sandbox Code Playgroud)

c++ rvalue rvalue-reference xvalue c++11

9
推荐指数
2
解决办法
1313
查看次数