我的 Return 类型需要定义吗?
Does my Return Type Need to be Defined?
Forward declaration 让我们推迟定义实际类型直到实现文件。这在 header 中允许用于指向前向声明类型的指针或引用。
即:
Returning by value does not require the type definition. A forward declaration is sufficient
有人可以用标准的实际报价来证实或否认这一点吗?我的印象是这不合法。
Returning by value does not require the type definition. A forward declaration is sufficient
按值声明 returns 的函数不需要类型定义。一个 well-formed 演示:
struct S;
S foo();
struct S {};
int main() {
foo();
}
S foo() {
return {};
}
定义或调用按值 return 的函数确实需要类型定义。标准草案 [basic.def.odr]:
5 Exactly one definition of a class is required in a translation unit if the class is used in a way that requires the class type to be complete. [ Example: ... [snip] ... [ Note: The rules for declarations and expressions describe in which contexts complete class types are required. A class type T must be complete if:
- [snip]
- 5.9 a function with a return type or argument type of type T is defined ([basic.def]) or called ([expr.call]), or
- [snip]
具有不完整 return 类型的函数的声明是隐式允许的,因为列表中的任何规则都没有禁止。
该规则在标准的后面 re-worded,并因异常 [dcl.fct] 而放宽(感谢 @cpplearner 指出了这条规则):
11 Types shall not be defined in return or parameter types. The type of a parameter or the return type for a function definition shall not be an incomplete (possibly cv-qualified) class type in the context of the function definition unless the function is deleted ([dcl.fct.def.delete]).
一个ill-formed演示:
struct S;
S foo() {
return {};
} // oops
struct S {};
另一个 ill-formed 演示:
struct S;
S foo();
int main() {
foo(); // oops
}
struct S {};
S foo() {
return {};
}
Forward declaration 让我们推迟定义实际类型直到实现文件。这在 header 中允许用于指向前向声明类型的指针或引用。
Returning by value does not require the type definition. A forward declaration is sufficient
有人可以用标准的实际报价来证实或否认这一点吗?我的印象是这不合法。
Returning by value does not require the type definition. A forward declaration is sufficient
按值声明 returns 的函数不需要类型定义。一个 well-formed 演示:
struct S;
S foo();
struct S {};
int main() {
foo();
}
S foo() {
return {};
}
定义或调用按值 return 的函数确实需要类型定义。标准草案 [basic.def.odr]:
5 Exactly one definition of a class is required in a translation unit if the class is used in a way that requires the class type to be complete. [ Example: ... [snip] ... [ Note: The rules for declarations and expressions describe in which contexts complete class types are required. A class type T must be complete if:
- [snip]
- 5.9 a function with a return type or argument type of type T is defined ([basic.def]) or called ([expr.call]), or
- [snip]
具有不完整 return 类型的函数的声明是隐式允许的,因为列表中的任何规则都没有禁止。
该规则在标准的后面 re-worded,并因异常 [dcl.fct] 而放宽(感谢 @cpplearner 指出了这条规则):
11 Types shall not be defined in return or parameter types. The type of a parameter or the return type for a function definition shall not be an incomplete (possibly cv-qualified) class type in the context of the function definition unless the function is deleted ([dcl.fct.def.delete]).
一个ill-formed演示:
struct S;
S foo() {
return {};
} // oops
struct S {};
另一个 ill-formed 演示:
struct S;
S foo();
int main() {
foo(); // oops
}
struct S {};
S foo() {
return {};
}