什么是 C++ 内联类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5388097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:03:01  来源:igfitidea点击:

What's the c++ inline class?

c++classinlineclang

提问by Eonil

I accidentally found that the Clang compiler allows :

我偶然发现 Clang 编译器允许:

inline class AAA
{
};

in C++. What's this?

在 C++ 中。这是什么?



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

附注。我将此报告给 Clang 邮件列表[email protected],现在等待回复。我会在收到通知后更新这个问题。

采纳答案by Eonil

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

我从 Clang 邮件列表中得到了答案。这是一个错误: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

这是对话:http: //lists.cs.uiuc.edu/pipermail/cfe-dev/2011-March/014207.html

回答by Tamer Shlash

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 inlinekeyword if you didn't declare any function.

此外,您应该注意在其他非法情况下出现的编译器错误(或警告),例如声明一个变量而不是A(),还要注意编译器声明inline如果您没有声明任何函数,它将忽略关键字。

Hope that's helpful.

希望这有帮助。

Edit : For The comment of Eonil

编辑:对于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

如果你在上面的问题中谈论你的代码,那么和我看到的情况一样,编译器会给你一个警告: '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, Bfor example, it will generate a compiler error : 'B' : 'inline' not permitted on data declarations

但是,如果您在我的答案中使用代码但替换A()为变量,B例如,它将生成编译器错误:'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

所以我们发现编译器接受这样的声明没有错误,尝试自己编写怎么样inline double;?它会产生一个警告:'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 inlineis not important at all.

因为的优先级inline根本不重要。

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.

但它们是相同的,因为 的优先级并不重要inline

Hope it's clear and convincing.

希望它清楚且令人信服。

回答by CB Bailey

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

clang 不允许这样做,inline只能用于函数声明,来自 ISO/IEC 14882:2003 7.1.2 [dcl.fct.spec] / 1 :

Function-specifierscan be used only in function declarations.

函数说明符只能在函数声明中使用。

inlineis one of three function-specifiers, virtualand explicitbeing the others.

inline是三个函数说明符之一virtual并且explicit是其他的。

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

正如@MatthieuM 所指出的,在下一版本的 C++ (C++0x) 中,inline命名空间定义中也允许使用关键字(与inline作为函数说明符的语义不同)。