开发者

Namespace using declaration (C++ Primer - Stanley Lipmann)

开发者 https://www.devze.com 2023-02-16 16:18 出处:网络
Anyone can help me to understand this statement found in chapter 3 (Library Types) by Stanley Lipmann?

Anyone can help me to understand this statement found in chapter 3 (Library Types) by Stanley Lipmann?

"Using an unqualified version of a namespace name without a using declaration is an error, although some compilers may fail to detect this error"

I'm h开发者_Go百科aving such hard time understanding the semantics of his sentence (english).

Is he trying to say something like the below scenario?

int main() {  
  xx::yy  
}

where xx is a namespace not defined using the "using" statement and yy is a member?


Example:

cout is a name of the std namespace. The unqualified name is cout. The qualified name is std::cout. It is an error to use the unqualified name(cout) without a using declaration beforehand. You can use either one of the two following declarations:

// This brings in the entire std namespace
using namespace std;

OR

// This only brings in cout.  You would still need to qualify other names,
// such as cin, endl, etc...
using std::cout;


What he's saying is that the following code should not compile:

#include <iostream>
void foo() {
  cout << "This is an error!" << endl;
}

The cout and endl names are not defined right now. They're declared as std::cout and std::endl, and in order to use them, you can do one of a few things:

#include <iostream>
void foo() {
  std::cout << "This, I think, is the best way to do it." << std::endl;
}

Using the fully qualified name prevents collisions later on: you'll never have something else called std::cout.

#include <iostream>
void foo() {
  using std::cout;
  using std::endl;
  cout << "This is pretty good." << endl;
}

Having the using statements specify the exact names you're using, and having the using statements in the function, can save some typing and makes collisions pretty unlikely.

#include <iostream>
using namespace std;
void foo() {
  cout << "This works, but isn't good." << endl;
}

Importing the entire std namespace makes it pretty likely that you'll end up having a function named the same as an std function.. You might discover that as soon as you write it, or you might write your function and then later include the header file with the std version of the function, at which point your application will mysteriously break.


A namespace name is the name of a namespace.

namespace A {

}

namespace B = A;

The statement says that using a namespace name without a using declaration is an error. But that's not true: The above code is fine, still using the namespace-name A as an unqualified name.

Probably it should say the following, to convey its meaning

"Using an unqualified version of a namespace member name without a using declaration outside the scope of the namespace is an error, although some compilers may fail to detect this error"

Mentioning the scope is important. The following, for example, is fine too, even though it uses the unqualified version of a namespace member name

namespace A {
  int x;
  int &y = x; // x is an unqualified name
}

Books should be careful to try and not use slippery language. And even outside the scope of the namespace, the above sentence is not entirely correct because you can also extend the scope of x by a using directive. Using declarations aren't the only way to name a namespace member outside the namespace using an unqualified name.

0

精彩评论

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