本文介绍了是否可以指定重复的C ++类范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意, Log :: Log 命名类构造函数,它只允许在某些上下文中,但只要结束 Log :: Log :: ... 使用日志(如 fn ),那么它不会命名构造函数。具体来说,§3.4.3.1/ 1a说:


I wouldn't expect this to compile but it does. Could this be a compiler bug, or does it have some correct meaning?

$ g++ -c scopes.cpp
$ cat scopes.cpp
class Log {
public:
    Log() { }
    static void fn() { }
};

void test() {
    Log::Log::Log::Log::Log::Log::fn();
}

$ g++ --version
g++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3
解决方案

Yes, it's legal. A class's name is inserted into its own namespace, which is called the injected-class-name. From C++03 §9/2:

Note that Log::Log names the class constructor, which is only allowed in certain contexts, but as long as you end the chain of Log::Log::... with something other than Log (such as fn), then it doesn't name the constructor. Specifically, §3.4.3.1/1a says:

这篇关于是否可以指定重复的C ++类范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 21:15
查看更多