开发者

What is the need to specify "std" prefix?

开发者 https://www.devze.com 2023-02-15 11:44 出处:网络
I\'m a beginner in programming and I often see many programs using the prefix std if they are using any std functions like std::cout, std::cin, etc. I was wondering what is it\'s purpose ? Is it just

I'm a beginner in programming and I often see many programs using the prefix std if they are using any std functions like std::cout, std::cin, etc. I was wondering what is it's purpose ? Is it just a way of good programming or is there more to it ? Does it make any difference for the com开发者_如何学Pythonpiler or is it readability or what ? Thanks.


The STL types and functions are defined in the namespace named std. The std:: prefix is used to use the types without fully including the std namespace.

Option 1 (use the prefix)

#include <iostream>

void Example() {
  std::cout << "Hello World" << std::endl;
}

Option #2 (use the namespace)

#include <iostream>
using namespace std;

void Example() {
  cout << "Hello World" << endl;
}

Option #3 (use types individually)

#include <iostream>
using std::cout;
using std::endl;

void Example() {
    cout << "Hello World" << endl;
}

Note: There are other implications to including an entire C++ namespace (option #2) other than not having to prefix every type / method with std:: (especially if done within a header) file. Many C++ programmers avoid this practice and prefer #1 or #3.


C++ has a concept of namespaces.

namespace foo {
  int bar();
}

namespace baz {
  int bar();
}

These two functions can coexist without conflict, since they're in different namespaces.

Most of the standard library functions and classes live in the "std" namespace. To access e.g. cout, you need to do one of the following, in order of preference:

  • std::cout << 1;
  • using std::cout; cout << 1;
  • using namespace std; cout << 1;

The reason you should avoid using is demonstrated with the above foo and baz namespaces. If you had using namespace foo; using namespace baz; any attempt to call bar() would be ambiguous. Using the namespace prefix is explicit and exact, and a good habit.


Nobody mentioned in their answer that a using namespace foo statement can be put inside a function body, thereby reducing namespace contamination in other scopes.

For example:

// This scope not affected by using namespace statement below.

void printRecord(...)
{
    using namespace std;
    // Frequent use of std::cout, io manipulators, etc...
    // Constantly prefixing with std:: would be tedious here.
}

class Foo
{
   // This scope not affected by using namespace statement above.
};

int main()
{
   // This scope not affected either.
}

You can even put a using namespace foo statement inside a local scope (pair of curly braces).


It's a C++ feature called namespaces:

namespace foo {
   void a();
}


// ...

foo::a();

// or:

using namespace foo;
a(); // only works if there is only one definition of `a` in both `foo` and global scope!

The advantage is, that there may be multiple functions named a - as long as they are within different namespaces, they can be used unambiguously (i.e. foo::a(), another_namespace::a()). The whole C++ standard library resides in std for this purpose.

Use using namespace std; to avoid the prefix if you can stand the disadvantages (name clashes, less clear where a function belongs to, ...).


It's short for the standard namespace.

You could use:

using namespace std

if you don't want to keep using std::cout and just use cout

0

精彩评论

暂无评论...
验证码 换一张
取 消