无法在函数定义的类外声明符中完全限定类名

v.o*_*dou 12 c++ methods parsing fully-qualified-naming

该程序导致不希望的贪婪解析的死胡同:

struct float4x4 {};
class C
{
    float4x4 M();
};

float4x4 ::C::M()
{
    return float4x4{};
}
Run Code Online (Sandbox Code Playgroud)

:8:1:错误:'float4x4'中没有名为'C'的成员; 您的意思仅仅是“ C”吗?
float4x4 :: C :: M()
^ ~~~~~~~~~~~~

可以使用尾随返回类型“固定”:

auto ::C::M() -> float4x4
{}
Run Code Online (Sandbox Code Playgroud)

现在一切都好。

因此,我认为在使用heading-return-type声明符语法时,我们不能完全限定类名吗?

for*_*818 10

您可以使用方括号来消除歧义:

float4x4 (::C::M)()
{
    return float4x4{};
}
Run Code Online (Sandbox Code Playgroud)

尽管我用gcc和clang(都是-pedantic)进行了测试,但我不能真正告诉您什么规则可以做到这一点。我更喜欢尾随返回类型。