通过引用传递匿名变量
Passing an anonymous variable by reference
标准 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" 只能接受由左值引用传递的参数。因此,传递匿名参数是错误的。
相关文章:
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 多个"常量引用"变量可以共享同一个内存吗?
- 如果非动态变量被指针引用,何时超出范围?
- 在 c++ 中将变量作为结构构造函数中的引用传递
- 在 gtest 中初始化堆栈上的引用变量的隔离错误
- 为什么C++可以使用未初始化的成员变量(引用或指针 *NOT 值复制*)来初始化其父类的成员变量
- 地址运算符是否返回变量引用的对象的地址
- 未在另一个函数中设置变量引用的问题
- 英特尔样式内联程序集和 AT&T 样式中的变量引用,C++
- 将成员变量引用为类成员
- 从静态变量引用非静态变量会使非静态变量先于静态变量进行析构函数
- 何时解析 C++11 lambda 表达式中的变量引用
- 堆栈/堆变量的变量/引用名称或类型存储在内存中的位置
- C++强制对变量引用进行编译优化,使之成为直接的
- Boost变量引用和相等比较
- 使复制的成员引用变量引用副本的成员,而不是原始成员的成员
- 为什么要在Java中生成变量引用数组
- 捕获和调试对移动lambda内部局部变量引用的无效使用
- 在变量初始化后更改变量引用
- 从'C'程序中提取变量定义和变量引用