4

I'm new to C++ and had a background in C. The one thing which is quite difficult for me to adopt is to frequently use scope operator e.g. std:: Well, i'd avoid it usage by putting using namespace std at the start of my source code but a lot of people doesn't use this method as they think this may bite them in future.

Plus, the visual-studio also shows error/warning messages along scope operator e.g.

cannot convert from 'std::vector<int,std::allocator<_Ty>> *' to 'std::shared_ptr<std::vector<int,std::allocator<_Ty>>>'

Though the above message is verbose but its such a pain to read it (?). I think it can be simple to read if it was in this form

cannot convert from 'vector<int,allocator<_Ty>> *' to 'shared_ptr<vector<int,allocator<_Ty>>>'

1) Why everyone is using std::, even for cout, cin, endl ? Why would anyone use the labels for some other purpose anyways?

2) Is their a workaround in Visual studio to not show me error/messages/syntax-highlights with a prefix std:: ?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Nouman Tajik
  • 433
  • 1
  • 5
  • 18
  • 3
    1) "_Though the above message is verbose but its such a pain to read it (?)._" How/Why? 2) [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) 3) "_Is their a workaround in Visual studio to not show me error/messages/syntax-highlights with a prefix `std::` ?_" What about user defined types/functions, which are defined in namespaces? Should they not have that qualifier? How would you, then, distinguish between those names, if namespace wouldn't be shown? – Algirdas Preidžius Jan 15 '20 at 11:38
  • Just think of the namespace as part of the name. – Pete Becker Jan 15 '20 at 14:21
  • Also, ask yourself what the error message without `std::` would mean if you had something in your code, outside any namespace, named `vector`. – Pete Becker Jan 15 '20 at 14:22

4 Answers4

5

Although, as pointed out in the comments, code like using namespace std; is considered bad practice, you can avoid repeated use of the namespace prefix in code like std::cout, by specifying individual scoped elements in using statements.

Something like this:

using std::cout;  //
using std::cin;   // You can, from then on, just use 'cout', 'cin' and 'endl'
using std::endl;  //

For very common elements, like those listed in the above code, you can put the relevant using lines in a header file - typically, your 'global' header that you use to build your precompiled header.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
3

For starters there is a difference in name lookup for qualified and unqualified names.

Consider for example the following program.

#include <iostream>

class A
{
private:    
    int x = 10;

public:
    operator int() const { return x; }
    friend void f( const A & )
    {
        std::cout << "f( const A & )\n";
    }
};

void f( int )
{
    std::cout << "f( int )\n";
}

int main() 
{
    const A a;

    f( a );

    ::f( a );

    return 0;
}

The program output is

f( const A & )
f( int )

Secondly using using directives can result in name collisions or selecting a wrong function due to the overload resolution.

Apart of this the code can be less readable because the reader will not know for example whether endl is std::endl or a user defined function defined in some namespace that is included in the name lookup due to numerous using directives.

There are situations when you need to use an explicit or an implicit using directive.

For example if you want to use declarations from the namespace std::placeholders or when you want to declare an inline namespace. But the scope of these directive is usually very limited. Another example is using using declarations. But again try to make their scopes as small as possible.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Inside a limited scope, for example within a function, it's logical to use using namespace std or any other using.

Repeating namespace names is tedious when you can avoid it at no cost. I 've seen lots of code infested with std::, especially when it comes to the usage of std::forward, std::move, std::function, std::thread etc.

C++ keywords like using are there for a reason; Unconditionally saying that "using namespace std is bad" is erroneous.

Michael Chourdakis
  • 10,345
  • 3
  • 42
  • 78
  • Opinions (based on individuals' experiences) differ, so calling your dissent here "erroneous" is misleading. Trying to convince people that an opinion you hold (which you are welcome to) is actually a fact (when it isn't one) is dirty pool. (That this is behavior I may myself be guilty of in the "spaces vs tabs" debate is irrelevant! ;). – Mark Storer Jan 15 '20 at 13:45
  • I'm not trying to convince anyone. – Michael Chourdakis Jan 15 '20 at 13:51
0

To answer your 2), I believe the answer is "No, there's no way to change the way VS writes out error messages to strain out all the std::".

The good news is that these error messages used to be much worse. You'd get error messages for all N variations on a given template, and then have to parse out the tiny spec of signal from the ocean of noise. Not fun. Heaven help you if you were using nested templates like std::vector<std::vector<std::string> > >.

So while template error messages can still be a bit verbose, the information presented is actual information, not spam spam spam eggs and spam without the eggs.

Oh, and >> used to always parse as a stream operator, so you had to add spaces between the >s for a nested template. UP HILL, BOTH WAYS!!!

Mark Storer
  • 15,672
  • 3
  • 42
  • 80