41

I accidentally found that the Clang compiler allows :

inline class AAA
{
};

in C++. What's this?


PS. I reported this to Clang mailing list cfe-dev@cs.uiuc.edu, and now waiting for reply. I'll update this question by I'm informed.

eonil
  • 83,476
  • 81
  • 317
  • 516
  • inline function makes sense....but inline class?? – mpen Mar 22 '11 at 07:39
  • 11
    "What's this?" - Something that doesn't exist in standard C++. The `inline` specifier is for functions, not classes. You may have stumbled upon a bug in the Clang C++ compiler. – In silico Mar 22 '11 at 07:39
  • 1
    Added `clang` tag, I could not find anything on the bug tracker, it might be worth opening one (you could always post a mail to cfe-dev [at] cs.uiuc.edu beforehand if you're unsure). If this is on the 2.9 line, they'll probably want to fix it soon. – Matthieu M. Mar 22 '11 at 07:43
  • 1
    This may be something that tells the compiler not to generate object-oriented code for the class you defined. Maybe it expands the class declaration and memory management into the user code to make speed faster. – Hossein Mar 22 '11 at 07:46
  • Thanks guys. I have reported this to `cfe-dev@cs.uiuc.edu`. And I'm waiting for reply. – eonil Mar 23 '11 at 04:04
  • 1
    They replied it's a bug. However it had been fixed already. – eonil Mar 25 '11 at 05:28

3 Answers3

47

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.

Archmede
  • 1,592
  • 2
  • 20
  • 37
Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
  • 5
    My eyes!! +1 though for a nice and cryptic example when this works. :) – Xeo Mar 22 '11 at 09:03
  • 5
    Could you elaborate WTF, is happening here? Why would I ever declare a function like that, and is that function a method of the class? – polemon Mar 22 '11 at 09:20
  • 1
    @polemon: This function is not a method of that class, it's a function on its own, you could declare funciton or variable immediately after the closing brace and before the semicolon `;` and it (the function) will return an object of that class (and the variable will be an object of that class). And for reason, I don't actually have any reason for functions, there may be some reasons for variables, yet I don't have any of them in mind right now. – Tamer Shlash Mar 22 '11 at 09:27
  • @Mr.TAMER's particular construct _might_ be legal (and it's _definitely_ crazy), but if there's no function, I think the compiler ought to _reject_ it, not warn about it. (And the clang version with Xcode4 does neither.) This still smells like a bug to me. Does anyone have chapter&verse from the standard on this? – Nicholas Knight Mar 22 '11 at 11:42
  • Great description. But unfortunately, I tried to compile exactly same code which I instanced. And Clang did compile it without any error. So obviously it's not this case. – eonil Mar 23 '11 at 03:49
  • @TamerShlash Is this allowed in c++11? I get some linker errors saying undefined reference. – Chef Pharaoh Jul 12 '18 at 17:24
  • 1
    Might be helpful to clarify that "It's allowed in case you wish to declare a **inline** function..." –  Jul 08 '21 at 20:05
16

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).

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
12

I got an answer from Clang mailing list. It was a bug: http://llvm.org/bugs/show_bug.cgi?id=3941

However it looks already been fixed in recent build. Thanks anyway :)

Here's the conversation: http://lists.cs.uiuc.edu/pipermail/cfe-dev/2011-March/014207.html

eonil
  • 83,476
  • 81
  • 317
  • 516