为什么不能在类范围内推导我的类静态自动函数的类型? [英] Why can't the type of my class-static auto function be deduced within the class scope?
问题描述
我正在尝试获取 auto
函数的返回类型。 此方法:
I'm trying to get the return type of an auto
function. This works:
auto foo(int bar)
{
return 0;
}
typedef std::result_of<decltype(foo)> foo_t;
很好,接下来是下一步:获取 static的返回类型类范围内的auto
函数。 这也有效:
Great, here's the next step then: getting the return type of a static auto
function in a class scope. This also works:
struct Foo
{
static auto foo(int bar)
{
return 0;
}
};
typedef std::result_of<decltype(Foo::foo)> foo_t;
但是这不起作用:
struct Foo
{
static auto foo(int bar)
{
return 0;
}
typedef std::result_of<decltype(Foo::foo)> foo_t;
};
GCC表示错误:在扣除前,使用'static auto Foo :: foo(int)' Clang说道:'auto',在定义之前不能使用带有推导返回类型的函数'foo'。为什么?
GCC says "error: use of 'static auto Foo::foo(int)' before deduction of 'auto'", Clang says "function 'foo' with deduced return type cannot be used before it is defined". Why?
推荐答案
虽然您编写代码的方式使之成为可能,但<$ c $的类内定义c> foo()仅在完全定义该类之后才能进行处理。好像您是这样写的:
While the way you have written the code makes it appear possible, the in-class definition of foo()
can only be processed after the class is fully defined. It is as if you wrote this:
struct Foo
{
static auto foo(int bar);
typedef std::result_of<decltype(Foo::foo)> foo_t;
};
auto Foo::foo(int bar)
{
return 0;
}
foo()$ c的定义允许$ c>使用
类Foo
中定义的类型,包括 foo_t
,这些类型可以是循环的。因此,类Foo
的定义不允许使用其成员函数的定义-仅使用其声明。
The definition of foo()
is allowed to use types defined in class Foo
, including foo_t
, which would be circular. Therefore, the definition of class Foo
is not allowed to use the definition of its member functions--only their declarations.
换句话说,您假设代码是从上到下完全评估的。不是。
In other words, you assume the code is fully evaluated from the top to the bottom. It is not.
这篇关于为什么不能在类范围内推导我的类静态自动函数的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!