What does use in C ++ mean?

Like:

using ::size_t; using ::fpos_t; using ::FILE; 

This is actually a question inspired by the comment on this question:

When do you not need to include a header file?

+4
source share
5 answers

This is called using an ad. There are actually two ways to use the using keyword. There is a third special form of using declarations used within class definitions , but I will focus on the general declaration of use here. (see below).

  • using ad
  • using directive

They have two very different effects. Using an ad declares the name an alias for another ad or ad set (if you must name a set of overloaded functions). The name is declared in the current scope. That is, you can also use it inside blocks

 int main() { using std::swap; // ... } 

This is very useful if you use the name very often locally, and you do not want its prefix for all purposes, and is also useful when implementing swap using a context-specific search idium.

A using the directive , names the namespace and does not declare any names. Instead, he will change the name search to find names that are not actually pronounced where they think. To search for an unqualified name, he finds the names declared in the encompassing namespace, which includes both the usage directive and the target namespace. All names declared in the target namespaces will be found:

 int cout; int main() { using namespace std; // cout << 1; ambiguous! } 

Here cout will be considered declared twice in the global namespace and will cause ambiguity ( :: covers both main and std ). In a qualified namelookup, he will build a transitive closure of the namespace with all namespaces named using directives.

 using namespace foo; int main() { ::c++; } 

c not only viewed in the global namespace, but also in the foo namespace and in the namespaces in which foo uses directives for and so on. If, however, the global namespace contains a direct declaration (including a declaration of use), this declaration will hide declarations found indirectly using directives:

 using namespace foo; int c; int main() { ::c++; // not ambiguous! } 

Use of declarations can appear in many places, including definitions within a class. Its meaning is similar to its value, elsewhere with an important limitation: it declares the name an alias for one or more declarations, but the declarations must be members of the base class. This is very useful for making names visible in a derived class that would otherwise be hidden with the same name as there.

 struct base { void f(); }; struct derived : base { using base::f; // name "f" declared in derived void f(int); // overloads the using declaration }; 

Now you can call df() . If there wasn’t an ad with an ad, then a search by name could find only one declaration f in derived and stop the search, and not enter the scope of the base class:

 derived d; df(); // invalid without the using declaration df(0); // valid with or without the using declaration // explicitly starting lookup in base: valid with or without the using declaration d.base::f(); 

It also allows you to change the accessibility of base class members, although you should use it sparingly :)

In practice, I found it useful to re-view the virtual member function:

 struct base { virtual void f(); virtual void f(int); }; struct derived : base { // using base::f; would solve it virtual void f() { ... } }; 

Oops - now df(0); is invalid because a name search only finds the n parameter f ! A usage directive would solve it. Note that if you declare a function declaration that has the same parameter types and constant as the explicit declaration (for example, f() in this case), then the explicit declaration will still hide the one used in the usage declaration, - so both In this case, the functions f() will not conflict.

An alternative to this is to use a non-virtual idiom interface

 struct base { void f() { do_f(); } void f(int) { do_f(0); } private: virtual void do_f(); virtual void do_f(int); }; struct derived : base { private: virtual void do_f() { ... } }; struct derived1 : derived { private: virtual void do_f(int) { ... } }; 

Now both df(0) and df() valid no matter what object you name it on.

+6
source

Unfortunately, the example you are looking at is unclear.

 using ::_Filet; 

As others have noted, the using declaration makes the name from the specified namespace available in the current namespace. There are no public namespaces in this file, so you are assuming that the current namespace is a global namespace, and also :: with nothing before it accesses the global namespace. So here we seem to be moving the name from the global namespace to the global namespace. What's up with that?

The answer is to use a macro:

 _STD_BEGIN 

This is defined as namespace std { . So using declarations make these names appear in the std , otherwise they would only be in the global namespace.

+7
source

using <some symbol> pull a character from your namespace into the current namespace. Assume the following code:

 namespace foo { // Assume you want to use std::string, you can either do std::string bar; // or using std::string; string bar; } 

As you can see, you can either qualify a character using its namespace (first line of code), or the second way. For characters that you use quite often, pulling them into a namespace usually makes the code more understandable, but if you have a conflict (let's say foo contains the string class, which is bad but can happen), qualifying it with the appropriate namespace can resolve the conflict.

The namespace :: is a special case because it refers to the global namespace; in this particular case, the functions you are talking about are C functions, and C is not aware of the C ++ namespaces, so they fall into the global namespace.

Namespaces in C ++ are a very powerful mechanism to avoid naming name conflicts, and I highly recommend using them in C ++.

+3
source

The 'using' keyword allows you to transfer names from the namespace to the current namespace. If you want to use the name inside the namespace without entering them in the current namespace, you will need to use the format <namespace name>::<name> , as shown below:

 std::cout &lt&lt "Hello World"; 

If cout is entered in the current namespace, you can use it as shown below:

 cout &lt&lt "Hello World"; 

The using keyword can be used in the following ways:

  • As the directive used ( using namespace <namespace name>; ):
 using namespace std; 

This casts all the names inside the std to the current namespace.

  • As a declaration use ( using <namespace>::<member name>; ):
 using std::cout; 

This only results in the name std::cout in the current namespace.

+3
source

using makes the name of the specified namespace available in the current namespace.

0
source

Source: https://habr.com/ru/post/1305352/


All Articles