C++ 为什么可以';我的类静态自动功能的类型不能在类范围内推导吗?

C++ 为什么可以';我的类静态自动功能的类型不能在类范围内推导吗?,c++,c++14,C++,C++14,我正在尝试获取auto函数的返回类型: 但是: structfoo { 静态自动foo(int-bar) { 返回0; } typedef std::foo_t的结果; }; GCC说“错误:在“auto”的推导之前使用“static auto Foo::Foo(int)”,Clang说“具有推导返回类型的函数“Foo”在定义之前不能使用”。为什么?虽然您编写代码的方式使之成为可能,但是foo()的类内定义只能在类完全定义后才能处理。就好像你写了这样的话: struct Foo { s

我正在尝试获取
auto
函数的返回类型:

但是:

structfoo
{
静态自动foo(int-bar)
{
返回0;
}
typedef std::foo_t的结果;
};

GCC说“错误:在“auto”的推导之前使用“static auto Foo::Foo(int)”,Clang说“具有推导返回类型的函数“Foo”在定义之前不能使用”。为什么?

虽然您编写代码的方式使之成为可能,但是
foo()
的类内定义只能在类完全定义后才能处理。就好像你写了这样的话:

struct Foo
{
    static auto foo(int bar);

    typedef std::result_of<decltype(Foo::foo)> foo_t;
};

auto Foo::foo(int bar)
{
    return 0;
}
structfoo
{
静态自动foo(int-bar);
typedef std::foo_t的结果;
};
自动Foo::Foo(整型条)
{
返回0;
}
foo()
的定义允许使用
class foo
中定义的类型,包括
foo\u t
,这将是循环的。因此,
类Foo
的定义不允许使用其成员函数的定义——只允许使用它们的声明


换句话说,您假设代码是从上到下完全计算的。它不是。

我想你想写
std::result_of::type
不是吗?@O'Neil有一个公平的机会,我显然不知道它是如何工作的……这是……的主题。据我所知,这是正确的,但“你假设代码从上到下都得到了充分的评估”有点不公平。
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;
};
struct Foo
{
    static auto foo(int bar);

    typedef std::result_of<decltype(Foo::foo)> foo_t;
};

auto Foo::foo(int bar)
{
    return 0;
}