Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/5388097/whats-the-c-inline-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
";
    }
}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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...