简体   繁体   English

如果未分配返回未定义对象类型引用的C ++函数的返回值,会发生什么?

[英]What should happen when the return value from a C++ function that returns a reference of an undefined object type is not assigned?

Consider the following code: 请考虑以下代码:

class Foo;

Foo& CreateFoo();


void Bar()
{
   CreateFoo();
}

In Visual Studio this will result in an error C2027 that Foo is an undefined type. 在Visual Studio中,这将导致错误C2027,即Foo是未定义的类型。 In most other compilers it compiles fine. 在大多数其他编译器中,它编译得很好。 It is only an issue if the return value of CreateFoo is not assigned. 如果未分配CreateFoo的返回值,则只是一个问题。 If I change the line to: 如果我将行更改为:

Foo& foo = CreateFoo();

it compiles fine in Visual Studio. 它在Visual Studio中编译得很好。 Also if Foo is defined rather than just forward-declared, then it will compile fine with no assignment. 此外,如果定义Foo而不是仅仅向前声明,那么它将编译正常而没有赋值。

Which should be the correct behavior? 哪个应该是正确的行为? Is there anything in the C++ standard that addresses this, or is this something that is left to the implementation? C ++标准中有什么可以解决这个问题,还是留给实现的呢? I looked and didn't see anything that talks about this. 我看了,没有看到任何谈论这件事。

Update: A bug report has been filed. 更新: 已提交错误报告。

This looks like the relevant part of the Standard (section 5.2.2): 这看起来像标准的相关部分(第5.2.2节):

A function call is an lvalue if the result type is an lvalue reference type or an rvalue reference to function type, an xvalue if the result type is an rvalue reference to object type, and a prvalue otherwise. 如果结果类型是左值引用类型或对函数类型的右值引用,则函数调用是左值;如果结果类型是对象类型的右值引用,则为xvalue,否则为prvalue。

If a function call is a prvalue of object type: 如果函数调用是对象类型的prvalue:

  • if the function call is either 如果函数调用是

    • the operand of a decltype-specifier or decltype-specifier的操作数或

    • the right operand of a comma operator that is the operand of a decltype-specifier , 逗号运算符的右操作数,它是decltype-specifier的操作数,

    a temporary object is not introduced for the prvalue. 不为prvalue引入临时对象。 The type of the prvalue may be incomplete. prvalue的类型可能不完整。 [ Note: as a result, storage is not allocated for the prvalue and it is not destroyed; [注意:因此,不为prvalue分配存储空间,也不会销毁存储空间; thus, a class type is not instantiated as a result of being the type of a function call in this context. 因此,由于在此上下文中是函数调用的类型,因此不会实例化类类型。 This is true regardless of whether the expression uses function call notation or operator notation (13.3.1.2). 无论表达式是使用函数调用符号还是运算符表示法(13.3.1.2),都是如此。 — end note ] [ Note: unlike the rule for a decltype-specifier that considers whether an id-expression is parenthesized (7.1.6.2), parentheses have no special meaning in this context. - 尾注] [注意:与decltype-specifier的规则不同,它考虑id-expression是否带括号(7.1.6.2),括号在此上下文中没有特殊含义。 — end note ] - 结束说明]

  • otherwise, the type of the prvalue shall be complete. 否则,prvalue的类型应完整。

Since this function result type is an lvalue reference type, the function call evaluates to an lvalue, and the completeness requirement does not apply. 由于此函数结果类型是左值引用类型,因此函数调用求值为左值,并且完整性要求不适用。

The code is legal, at least in C++11, which no released version of Visual C++ implements fully. 代码是合法的,至少在C ++ 11中,没有发布的Visual C ++版本完全实现。

You can always use incomplete types in function declarations (since that only declares a signature of the function, not any real code), but not when you use it. 您总是可以在函数声明中使用不完整的类型(因为它只声明函数的签名,而不是任何实际代码),但在使用它时则不然。

Calling CreateFoo(); 调用CreateFoo(); is equals to (void) CreateFoo(); 等于(void) CreateFoo(); , and my guess is that Visual Studio needs to inspect the code of Foo to do ANY conversion (I'm not sure if you can actually write a void conversion), because, for conversions you need a complete type. ,我的猜测是Visual Studio需要检查Foo的代码来进行任何转换(我不确定你是否真的可以编写一个void转换),因为对于转换你需要一个完整的类型。

As for Foo & foo = CreateFoo(); 至于Foo & foo = CreateFoo(); , this does not do any conversions, so you can get away with having an incomplete type. ,这不会进行任何转换,因此您可以使用不完整的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将左值分配给右值引用时会发生什么? 没有破坏临时对象? - What happen when a lvalue assigned to a rvalue reference? No destruction of the temporary object? 当您从 c++ function 返回引用时会发生什么? - What happens when you return a reference from a c++ function? C ++,从具有引用返回类型的函数返回什么以指示错误 - C++, What to return from a function with reference return type to indicate error 在c ++对象上使用extern时的未定义引用,但不是整数类型 - Undefined reference when using extern on a c++ object, but not integral type 从C函数调用时C ++函数的未定义引用 - Undefined reference for a C++ function when called from C function 当函数没有返回值时,返回类型应该是什么? - What should the return type be when the function might not have a value to return? 当函数的返回类型是引用时返回此指针(C++) - Returning this pointer when the return type of a function is a reference (C++) 当我声明一个引用变量时,堆栈会发生什么? C ++ - What happen to stack when i declare a reference variable? C++ C ++,何时应返回引用? - C++, when should return reference? 当函数的返回值是指针并且返回的类型是c ++中的引用时,会发生什么? - what happens when the function returning value is a pointer and the returning type is a reference in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM