简体   繁体   English

isalnum的范围解析运算符

[英]Scope resolution operator for isalnum

I am asking this as a follow-up to this question. 我正在问这个问题,作为对此问题的后续行动。 The previous question was asked almost three years ago, so I though asking a new one would be better. 上一个问题是在大约三年前提出的,所以我问一个新的问题会更好。

The crux of that question I linked to is that the OP tried to run the following line of code: 我链接到的问题的症结在于,OP试图运行以下代码行:

find_if(s.begin(), s.end(), isalnum);

on some container s . 一些容器s The line of code failed to compile, and the OP should have done this 该行代码无法编译,OP应该执行此操作

find_if(s.begin(), s.end(), ::isalnum);

The accepted answer states that there are isalnum functions in the locale and cctype libraries, and that the compiler is having trouble disambiguating between the two, hence the :: scope resolution operator. 可接受的答案表明,在localecctype库中存在isalnum函数,并且编译器在消除两者之间的歧义时遇到了麻烦,因此::范围解析运算符存在问题。 I tested it by including only one of those libraries, and the compiler is still having issues disambiguating. 我仅通过其中一个库进行了测试,而编译器仍然存在歧义。 Why is that? 这是为什么? If I included only one of those libraries, then obviously the compiler shouldn't "know" about the other library, so why the conflict? 如果我只包括那些库中的一个,那么显然编译器不应该“知道”另一个库,那么为什么会发生冲突呢?

The second part of my question, is how does the :: operator tell us which isalnum function we want? 我的问题的第二部分是::运算符如何告诉我们我们想要哪个isalnum函数?

Thanks 谢谢

EDIT 编辑

I know that the :: operator tells us that the function/variable we want is in the global scope, but that still doesn't answer my second question. 我知道::操作符告诉我们我们想要的函数/变量在全局范围内,但这仍然无法回答我的第二个问题。

The isalnum from <locale> is defined in namespace std . <locale>isalnum在命名空间std定义。 The isalnum from <cctype> is defined in namespace std and globally, because symbols from the C library are (probably [1]) made available in global scope. <cctype>isalnum是在命名空间std 全局中定义的,因为C库中的符号(可能[1])在全局范围内可用。 Using ::isalnum requests the version of isalnum from the global scope, and not from namespace std . 使用::isalnum请求的版本isalnum从全球范围内,而不是从namespace std

[1]. [1]。 The standard guarantees that if you include <ctype.h> then the symbols will be at global scope. 该标准保证,如果包含<ctype.h>则这些符号将位于全局范围内。 For the C library headers, this is almost always the case for the cc* versions too, although strictly it is implementation defined. 对于C库头文件,对于cc*版本,几乎也总是如此,尽管严格来说是实现定义的。

From my understanding using the scope resolution operator (::) helps identify the scope of your program. 根据我的理解,使用范围解析运算符(::)有助于确定程序的范围。

So if you had: 因此,如果您有:

Add();     //This is in Global Scope

class Test{
    void Add();       //This is in scope of the "Test" class
    int useAdd(); { ::Add();}
}

So in this case the useAdd() function refers to the add function in global scope rather than the add function within the test class. 因此,在这种情况下, useAdd()函数引用全局范围内的add函数,而不是测试类中的add函数。 If you wanted to refer to the one in the test class using the scope resolution operator you would put Test::Add() rather than ::Add() . 如果要使用范围解析运算符引用测试类中的那个,则应放置Test::Add()而不是::Add()

In the above case it may be that he had the "isalnum" function implemented both in his current class as well as a global function and he needed to specifically refer to the global function rather than the class function. 在上述情况下,可能是他在当前类以及全局函数中都实现了“ isalnum”函数,并且他需要专门引用全局函数而不是类函数。

EDIT: I think I misunderstood the question, Andrew has answered it better than myself. 编辑:我想我误解了这个问题,安德鲁比我自己回答得更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM