What does it mean in C++ when you have a parameter like ::lower?

Failed Software Developer

On S.O. posts such as How to convert std::string to lower case?, I've seen the syntax ::something, e.g.

std::transform(data.begin(), data.end(), data.begin(), ::tolower);

and I'm wondering what that means. I know that std::transform looks for a function or variable named transform inside the namespace std. But what does the scope operator mean when it doesn't have a first argument?

Vlad from Moscow

But what does the scope operator mean when it doesn't have a first argument?

It denotes the global namespace.

Consider a simple example. Let;s assume that you defined function swap. At the same time you included header <utility> that also contains a declaration of swap. and after it dirrective using namespace std; To distinguihs them you need to use qualified name.

#include <iostream>
#include <utility>

using namespace std;


void swap( int &x, int &y )
{
    std::cout << "Inside ::swap" << std::endl;
    int tmp = x;
    x = y + 10;
    y = tmp + 10;
}

int main() 
{
    int x = 1;
    int y = 2;

    std::swap( x, y );

    std::cout << "x = " << x << ", y = " << y << std::endl;

    x = 1;
    y = 2;

    ::swap( x, y );

    std::cout << "x = " << x << ", y = " << y << std::endl;


    return 0;
}

The output will be

x = 2, y = 1
Inside ::swap
x = 12, y = 11

In this example ::swap refers to your function swap defined in the global namespace while std::swap refers to standard function swap declared in name space std in header <utility>

If you will write unqualified name

swap( x, y );

when the compiler will not know which function to call: either standard function std::swap or your own function ::swap

If to exclude directive

using namepsace std;

and write

swap( x, y );

then the compiler will call your own function swap.

Now I will explain why in your example with standard algorithm std::transform there is used ::tolower. The problem is that in C++ there are two functions with name tolower. One of which is C standard function declared in header <cctype> and other is C++ function declared in header <locale>. The C++ Standard allows to place C standard functions in the global namespace. So using name ::tolower allows the compiler to select the C standard function instead of the C++ function.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

What does it mean when you bind something to a timeout?

分類Dev

In C, what exactly does something like "a string"[4] mean and signify?

分類Dev

What does the last parameter of glVertexAttribPointer mean?

分類Dev

What does ::* mean in C++?

分類Dev

What does ... (ellipsis) as one and only function parameter in a function prototype mean in C++?

分類Dev

What does `unsigned` in MySQL mean and when to use it?

分類Dev

What does it mean when there are variables equal to a function?

分類Dev

What does 'a' mean in JVM instructions like 'aload' or 'areturn'?

分類Dev

What does errors like "!=<" mean in agda and how to fix

分類Dev

What does "No valid rules have been specified for JavaScript files" mean?

分類Dev

What does error: invalid operands to binary % (have ‘float’ and ‘int’) mean

分類Dev

What does object?.Property mean in c#

分類Dev

What does this mean by variable define in C

分類Dev

What does char* (int) mean in C++?

分類Dev

what does condition if(k & 1) mean in c?

分類Dev

What does 'this' mean in a c# constructor?

分類Dev

What does extra parameter mean in this for-loop in Python?

分類Dev

What does () mean as an argument in a function where a parameter of type T is expected?

分類Dev

What does it mean to pass a pointer parameter by reference to a function?

分類Dev

What does "<built-in method lower of str object at [hex numbers] >" mean?

分類Dev

Scala lower bound type parameter does not work when type argument is not specified explicitly

分類Dev

Angular: What do you do when you need to 'track by' but you don't have a unique property to track by?

分類Dev

What does it mean when code is executed in [kernel|user] mode?

分類Dev

What does it mean when Apple "hosts" my IAPs?

分類Dev

What does these parameters mean in jupyter notebook when I input "%%time"?

分類Dev

What does "Client" mean when referencing SOLID principles?

分類Dev

What does it mean when folders/files are grey in Netbeans 10?

分類Dev

What does MKErrorDomain code=3 mean when using the MapKit API?

分類Dev

What does it mean when a value is passed through a next function?

Related 関連記事

  1. 1

    What does it mean when you bind something to a timeout?

  2. 2

    In C, what exactly does something like "a string"[4] mean and signify?

  3. 3

    What does the last parameter of glVertexAttribPointer mean?

  4. 4

    What does ::* mean in C++?

  5. 5

    What does ... (ellipsis) as one and only function parameter in a function prototype mean in C++?

  6. 6

    What does `unsigned` in MySQL mean and when to use it?

  7. 7

    What does it mean when there are variables equal to a function?

  8. 8

    What does 'a' mean in JVM instructions like 'aload' or 'areturn'?

  9. 9

    What does errors like "!=<" mean in agda and how to fix

  10. 10

    What does "No valid rules have been specified for JavaScript files" mean?

  11. 11

    What does error: invalid operands to binary % (have ‘float’ and ‘int’) mean

  12. 12

    What does object?.Property mean in c#

  13. 13

    What does this mean by variable define in C

  14. 14

    What does char* (int) mean in C++?

  15. 15

    what does condition if(k & 1) mean in c?

  16. 16

    What does 'this' mean in a c# constructor?

  17. 17

    What does extra parameter mean in this for-loop in Python?

  18. 18

    What does () mean as an argument in a function where a parameter of type T is expected?

  19. 19

    What does it mean to pass a pointer parameter by reference to a function?

  20. 20

    What does "<built-in method lower of str object at [hex numbers] >" mean?

  21. 21

    Scala lower bound type parameter does not work when type argument is not specified explicitly

  22. 22

    Angular: What do you do when you need to 'track by' but you don't have a unique property to track by?

  23. 23

    What does it mean when code is executed in [kernel|user] mode?

  24. 24

    What does it mean when Apple "hosts" my IAPs?

  25. 25

    What does these parameters mean in jupyter notebook when I input "%%time"?

  26. 26

    What does "Client" mean when referencing SOLID principles?

  27. 27

    What does it mean when folders/files are grey in Netbeans 10?

  28. 28

    What does MKErrorDomain code=3 mean when using the MapKit API?

  29. 29

    What does it mean when a value is passed through a next function?

ホットタグ

アーカイブ