在扣除 auto 之前使用 decltype(auto) <func>

use of decltype(auto) <func> before deduction of auto

以下情况:

#include <stdint.h>

class C
{
public:
    C()=default;
    ~C()=default;
    template<uint8_t> struct integconst
    {
    };

    int m1(integconst<8>);
    int m2(integconst<8>);
    decltype(auto) masterMethod(int opMode);
private:
};

int C::m1(integconst<8>) {return 1;}
int C::m2(integconst<8>) {return 2;}

decltype(auto) C::masterMethod(int opMode)
{
    switch(opMode)
    {
    case 1:
            return m1(integconst<sizeof(uintptr_t)>{}); break;
    case 2:
            return m2(integconst<sizeof(uintptr_t)>{}); break;
    default:
            return m1(integconst<sizeof(uintptr_t)>{}); break;
    }
}

int main()
{
    C obj;
    int bla=obj.masterMethod(1);
    return 0;
}

输入上面的简化示例将没有问题。但是当我尝试在单独的文件 (full example here) 中分离实现和声明时,我得到了错误

main.cpp: error: use of 'decltype(auto) C::masterMethod(int)' before deduction of 'auto'。将实现直接移动到 class 本身解决了问题(或在同一文件中实现方法),但我真的不明白为什么?有人能解释一下为什么编译器还不知道 decltype(auto) 的类型吗,特别是编译器什么时候开始解析 "auto" 的 return 类型?如何像我想的那样将实现和声明分开?

如果您正在使用 return 类型推导,则不能将声明及其定义分离到不同的文件中。在编译时,编译器必须能够知道 'masterMethod' returns.

是做什么的

我认为这类似于以下问题:

每个源文件(.cpp 文件或 .cc 文件)都必须独立存在。标准将这些文件称为 翻译单元,因为程序的行为是相同的 as-if 所有这些文件都被翻译成单独的机器语言单位,然后才将机器代码链接到一个程序中。

Standing "on its own"表示源文件连同包含在其中的所有文件都必须传达翻译源文件的所有信息。如果将 masterMethod 放在一个源文件中,而将 main 放在另一个源文件中,编译器在编译 main.

您问题的答案

How would one seperate implementation and declaration as I would like to do it?

就是这样:要么把函数的源代码放在头文件里,要么放弃使用return类型推导。如果把源代码放到头文件里,就不用在里面定义class,只要声明成inline即可。