通过引用传递匿名变量

Passing an anonymous variable by reference

本文关键字:变量 引用      更新时间:2023-10-16

标准 int 或 char 等C++类型都有 ctors,因此您可以拥有如下表达式:

int a = int(67); // create anonymous variable and assing it to variable a
int b(13);       // initialize variable b
int(77);         // create anonymous variable

用户定义的类型(结构或类)能够执行相同的操作:

struct STRUCT
{
  STRUCT(int a){}
};
STRUCT c = STRUCT(67);
STRUCT d(13);
STRUCT(77);

问题是:为什么我们可以传递引用匿名结构或类实例,但不能传递标准类型?

struct STRUCT
{
  STRUCT(int a){}
};
void func1(int& i){}
void func2(STRUCT& s){}
void func3(int i){}
void func4(STRUCT s){}
void main()
{
  //func1(int(56));  // ERROR: C2664
  func2(STRUCT(65)); // OK: anonymous object is created then assigned to a reference
  func3(int(46));    // OK: anonymous int is created then assigned to a parameter
  func4(STRUCT(12)); // OK: anonymous object is created then assigned to a parameter
}

如果您的编译器允许这样做,那么它就不是标准兼容的C++编译器。不能将临时右值绑定到非常量左值引用。这是规则。clang 和 gcc 都不会为 func2(STRUCT(65)); 编译该代码。

相反,您有其他选择:

void func1(int&& i){}
void func1(const int& i){}

来自 C++03 的遗留内容:对非常量类型 (int &i) 的 (lvalue) 引用应该能够更改参数然后传递临时对象(如 56)是不合逻辑的,因为它不可更改。对 const 类型 (const int &i) 的引用应该只是将值视为只读,然后传递一个临时值(如 52)是合法的。

在 C++11 中,您可以通过 && 引用非常量临时对象。

您似乎正在使用具有此类错误的MS VC++编译器:)您必须使用常量引用绑定临时对象。例如,你可以写

const int &ri = 10;

但你可能不会写

int &ri = 10;

这同样适用于用户定义类型。

const STRUCT &rs = STRUCT( 10 );
STRUCT &rs = STRUCT( 10 ); // the compiler shall issue an error.

在 c++ 中,匿名临时对象始终是正确值。要接受右值作为参数,您可以:

1).空foo1(类型); 按值
传递2).void foo2(常量类型和);通过常量引用
传递3).空隙foo3(类型和&); 在 C++11 中,通过右值引用传递

您的"func3"和"func4"接受按值传递的参数,没关系。
但是,"func1" 和 "func2" 只能接受由左值引用传递的参数。因此,传递匿名参数是错误的。