Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the c++ inline class?

I accidentally found that the Clang compiler allows :

inline class AAA { }; 

in C++. What's this?


PS. I reported this to Clang mailing list [email protected], and now waiting for reply. I'll update this question by I'm informed.

like image 720
eonil Avatar asked Mar 22 '11 07:03

eonil


People also ask

What is inline in C?

In an inline function, a function call is replaced by the actual program code. Most of the Inline functions are used for small computations. They are not suitable for large computing. An inline function is similar to a normal function. The only difference is that we place a keyword inline before the function name.

What is an inline class C++?

C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.

What does class inline mean?

Inline classes are a subset of value-based classes. They don't have an identity and can only hold values. To declare an inline class, use the value modifier before the name of the class: value class Password(private val s: String)

What is static inline in C?

Static inline functions are simple. Either a function defined with the inline function specifier is inlined at a reference, or a call is made to the actual function. The compiler can choose which to do at each reference. The compiler decides if it is profitable to inline at -xO3 and above.


2 Answers

It's allowed in case you wish to declare a function that returns an object of that class directly after the class' declaration, for example :

#include <iostream>  inline class AAA  { public:     AAA()     {         // Nothing     }      AAA(const AAA& _Param)     {         std::cout << "Calling Copy Constructor of AAA\n";     } }A()  {      AAA a;      return a;  };  int main() {     A();     return 0; } 

Also you should notice the compiler errors (or warnings) that appear in other illegal cases, such as declaring a variable instead of A(), also notice that the compiler states that it ignores the inline keyword if you didn't declare any function.

Hope that's helpful.

Edit : For The comment of Eonil

If you are talking about your code above in the question, then it's the same case as I see, the compiler will give you a warning : 'inline ' : ignored on left of 'AAA' when no variable is declared

However, if you use the code in my answer but replace A() with a variable, B for example, it will generate a compiler error : 'B' : 'inline' not permitted on data declarations

So we find that the compiler made no mistake with accepting such declarations, how about trying to write inline double; on its own? It will generate a warning : 'inline ' : ignored on left of 'double' when no variable is declared

Now how about this declaration :

double inline d() { } 

It gives no warnings or errors, it's exactly the same as :

inline double d() { } 

since the precedence of inline is not important at all.

The first code (in the whole answer) is similar to writing :

class AAA {     // Code };  inline class AAA A() {     // Code } 

which is legal.

And, in other way, it can be written as :

class AAA {     // Code };  class AAA inline A() {     // Code } 

You would be relieved if you see the first code (in the whole answer) written like :

#include <iostream>  class AAA  {     // Code } inline A()  {     // Code  }; 

But they are the same, since there is no importance for the precedence of inline.

Hope it's clear and convincing.

like image 109
Tamer Shlash Avatar answered Sep 22 '22 09:09

Tamer Shlash


clang shouldn't allow this, inline can only be used in the declaration of functions, from ISO/IEC 14882:2003 7.1.2 [dcl.fct.spec] / 1 :

Function-specifiers can be used only in function declarations.

inline is one of three function-specifiers, virtual and explicit being the others.

As @MatthieuM notes, in the next version of C++ (C++0x), the inline keyword will also be allowed in namespace definitions (with different semantics to inline as a function-specifier).

like image 40
CB Bailey Avatar answered Sep 21 '22 09:09

CB Bailey