Please tell me the difference between using indicator and using declaration!

Asked 2 years ago, Updated 2 years ago, 26 views

I started studying c++, but I was stuck while studying namespace. crying While I was googling about my questions, I couldn't find the answer, and I found a question posted by someone who had exactly the same question as me. The following is an edited version of the question.

Say for example there are 100 name definitions in the namespace std, if I were to include all of them using the "using" declaration (Option1), it still would not be the same as "using" directive (Option2)

Option1 - using declaration Code: using std::cout; using std::cin; .... .... //Assume I mention all the rest of the name definition in std using std::

Option2 - using directive Code: using namespace std; Now option1 and option2 (assuming they are specified in the same point in the code) aren't the same, I would like to know how / why they are different.

assuming there are 2 namesaces "Hello" and "GoodBye" and both have a function message().

Test1 - Following is legal: Code:

using namespace Hello; using namespace GoodBye; the above would present a problem ONLY when the function message() is called.

Test2 - Following is illegal: Code: using Hello::message; using GoodBye:: message;

this is probably the proof that using "using directive" is not same as multiple application of "using delcaration"s.

The question, after all, is why the using indicator is different from applying the using declaration many times, and how is it different?

c++

2022-09-22 21:10

1 Answers

I understand that there is no difference between the declaration of using and the application of using indicator in terms of using.

However, I would like to talk about the difference between the using indicator that I know and the using declaration.

Using indicator: All names of the specified namespace can be imported into the area where this declaration is located and used immediately without any affiliation designation. Ex) using namespace~

Using declarator: ex) using std::cout;

The Using declaration is considered an error if the name conflicts with the local variable (If the name of the region variable is the same as the name of Using, the compiler will not be able to distinguish.)

The Using indicator has limited visibility into the namespaces and does not issue errors or warnings. (If the name of the region variable is the same as the name of using, the compiler recognizes it as the region variable if it uses the same name within the region. To write the name of Namespace, you need to write in the form std::cout.)

At first glance, the using indicator seems more generous, but in fact, this situation can be the cause of a troubling error.


2022-09-22 21:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.