Search code examples
c++parsingmethodsfully-qualified-naming

Impossible to fully qualify class-name in out-of-class declarator of function definition


This program results in an undesired parsing greediness dead-end:

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

float4x4 ::C::M()
{
    return float4x4{};
}

:8:1: error: no member named 'C' in 'float4x4'; did you mean simply 'C'?
float4x4 ::C::M()
^~~~~~~~~~~~

Which can be 'fixed' using trailing return type:

auto ::C::M() -> float4x4
{}

now all good.

So I take it we can't fully qualify the class-name when using heading-return-type declarator syntax?


Solution

  • You can put brackets to disambiguate:

    float4x4 (::C::M)()
    {
        return float4x4{};
    }
    

    I cannot really tell you what rule makes this ok, while it is not without the brackets, though I tested with gcc and clang (both -pedantic). I would prefer the trailing return type.