Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 913
v.oddou Avatar asked Nov 18 '19 11:11

v.oddou


People also ask

What is class how function is defined outside the class?

Member functions of a class can be defined either outside the class definition or inside the class definition. In both the cases, the function body remains the same, however, the function header is different.

Can you declare a class in a function?

A class declaration can appear inside the body of a function, in which case it defines a local class. The name of such a class only exists within the function scope, and is not accessible outside.

Can you access member function outside class?

Member functions and static members can be defined outside their class declaration if they have already been declared, but not defined, in the class member list. Nonstatic data members are defined when an object of their class is created. The declaration of a static data member is not a definition.

What is class declaration in c++?

A class declaration creates a unique type class name. A class specifier is a type specifier used to declare a class. Once a class specifier has been seen and its members declared, a class is considered to be defined even if the member functions of that class are not yet defined.


1 Answers

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.

like image 75
463035818_is_not_a_number Avatar answered Sep 30 '22 05:09

463035818_is_not_a_number