12

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?

v.oddou
  • 6,476
  • 3
  • 32
  • 63
  • 2
    As long as C++ ignores the whitespace around `::`, I suppose there is no other way. – Yksisarvinen Nov 18 '19 at 11:17
  • @Someprogrammerdude yep, check this out https://godbolt.org/z/mt6GHD – v.oddou Nov 18 '19 at 11:18
  • 3
    The `::C` part looks like a workaround for some other issue. Otherwise it could have been simply `C` (like the compiler suggests;) – rustyx Nov 18 '19 at 11:23
  • 2
    @rustyx it's just mechanical re-emission of code by a transpiler that doesn't want to execute an ultra complex "find least qualified name" at this position. emitting a FQ-name bypasses lookup completely and is convenient for such tools. – v.oddou Nov 18 '19 at 11:25

1 Answers1

10

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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    incredible. indeed we can. https://godbolt.org/z/KCFbJZ that's disgusting but it's awesome. Of course in terms of style the trailing is great. But in my case I target a dialect that doesn't support it. – v.oddou Nov 18 '19 at 11:22