我可以写一个 returns 函数的函数类型吗?
Can I write a function type that returns a function?
以下在 gcc 和 clang 上编译失败
#include <type_traits>
int foo();
int main()
{
using R = std::result_of_t<decltype(foo)()>; // error
}
两个编译器的错误都涉及声明函数返回函数的非法性。但我并没有声明这样一个函数——我只是想写它的类型——因为那是 result_of
所期望的。这真的还是病态的吗?
您正在传递 type-id,它在 [dcl.name] 中定义为
[…] syntactically a declaration for a variable or function of that type that omits the name of the entity. […] It is possible to identify uniquely the location in the abstract-declarator where the identifier would appear if the construction were a declarator in a declaration. The named type is then the same as the type of the
hypothetical identifier.
要使假设的标识符具有某种类型,假设的声明首先必须是格式正确的。但这与 [dcl.fct]/10. Hence the program is ill-formed (and the compilers' error messages are actually comprehensible). This case is also more directly covered by [temp.deduct]/(8.10) 不同,暗示这是一个(SFINAE 友好的)错误。
事实上,仅暗示无效类型的用法就足以使程序格式错误。例如。创建指向函数返回函数的类型指针格式不正确:
using f = int();
using t = f(*)();
以下也是:
struct A {virtual void f() = 0;};
using t = A(*)();
(Clang 不应该接受这个。C.f。GCC 错误 17232 的有趣讨论)。
以下在 gcc 和 clang 上编译失败
#include <type_traits>
int foo();
int main()
{
using R = std::result_of_t<decltype(foo)()>; // error
}
两个编译器的错误都涉及声明函数返回函数的非法性。但我并没有声明这样一个函数——我只是想写它的类型——因为那是 result_of
所期望的。这真的还是病态的吗?
您正在传递 type-id,它在 [dcl.name] 中定义为
[…] syntactically a declaration for a variable or function of that type that omits the name of the entity. […] It is possible to identify uniquely the location in the abstract-declarator where the identifier would appear if the construction were a declarator in a declaration. The named type is then the same as the type of the hypothetical identifier.
要使假设的标识符具有某种类型,假设的声明首先必须是格式正确的。但这与 [dcl.fct]/10. Hence the program is ill-formed (and the compilers' error messages are actually comprehensible). This case is also more directly covered by [temp.deduct]/(8.10) 不同,暗示这是一个(SFINAE 友好的)错误。
事实上,仅暗示无效类型的用法就足以使程序格式错误。例如。创建指向函数返回函数的类型指针格式不正确:
using f = int();
using t = f(*)();
以下也是:
struct A {virtual void f() = 0;};
using t = A(*)();
(Clang 不应该接受这个。C.f。GCC 错误 17232 的有趣讨论)。