tags:

views:

263

answers:

5

Like :

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

In fact it's a question inspired by the comment under this question:

http://stackoverflow.com/questions/2532412/when-is-h-not-needed-to-include-a-header-file

+3  A: 

using <some symbol> pull a symbol from its 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 the symbol using its namespace (first line of code) or the second way. For symbols you use quite often, pulling them into the namespace tends to make the code a little more readable but if you have a conflict (say, foo contains a string class of its own which is bad practise but might happen), qualifying it with the appropriate namespace will allow you to resolve the conflict.

The namespace :: is a special case as it refers to the global namespace; in this particular case, the functions you're referring to are C functions and C doesn't know about C++ namespaces, so they end up in the global namespace.

Namespaces in C++ are a very powerful mechanism to avoid symbol naming clashes and I'd strongly encourage any C++ programmer to use them.

Timo Geusch
+1  A: 
jasonline
+6  A: 

Unfortunately the example you're looking at is obscure.

using ::_Filet;

As others have noted, the using declaration makes a name from the specified namespace available in the current namespace. In that file there appear to be no namespaces opened, so you'd assume the current namespace is the global namespace, and also the :: with nothing before it addresses the global namespace. So here we seem to be moving a name from the global namespace into the global namespace. What's up with that?

The answer is in the use of a macro:

_STD_BEGIN

This is defined as namespace std {. So what the using declarations are doing is making those names appear in the std namespace, where otherwise they would only be in the global namespace.

Daniel Earwicker
+1  A: 

This is called using declaration. There are actually two ways you can use the using keyword. There is a third special form of using declarations used inside class definitions, but i'll focus on the general using declaration here. (see below).

  • using declaration
  • using directive

These have two very different effects. A using declaration declares a name to be an alias to another declaration or a set of declarations (if you were to name a set of overloaded functions). The name is declared in the current scope. That is, you can use it inside blocks too

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

This is quite useful if you use a name very often locally and you don't want to prefix it in all uses, and it's also useful in implementing the swap using argment dependent lookup idiom.

A using directive names a namespace and does not declare any names. Instead it will modify name lookup to find names that aren't really declared where it thinks they are. For unqualified name lookup, it find names declared in the enclosing namespace that encloses both the using directive and the target namespace. All names that are declared in the target namespaces will be found:

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

Here, cout will be thought as being declared twice in the global namespace, and causes an ambiguity (:: encloses both main and std). In qualified namelookup, it will build the transitive closure of a namespace with all the namespaces named in using directives.

using namespace foo;

int main() {
  ::c++; 
}

c is not only looked up in the global namespace, but also in the namespace foo and in the namespaces that foo has using directives for and so on. If however the global namespace would contain a direct declaration (including a using declaration), that declaration will hide the declarations found indirectly by using directives:

using namespace foo;
int c;

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

Using declarations can appear in many places, including inside class definitions. Its meaning is similar to its meaning otherwhere with an important restriction: It declares a name to be an alias to one or more declarations, but the declarations must be members of a base class. This is very useful for making names visible in a derived class that would otherwise be hidden by the same name declared 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 d.f(). If there were no using declaration, then name lookup would only find one declaration of f in derived and stop lookup, not delving into the base class scope:

derived d;
d.f(); // invalid without the using declaration
d.f(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 to change the accessibility of base-class members, although you should use that sparingly :)

In practice, i found it useful for making virtual member function re-visible:

struct base {
  virtual void f();
  virtual void f(int);
};

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

Oops - now d.f(0); is invalid because name lookup only finds the zero parameter f! The using directive would solve it. Notice that if you alias a function declaration that has the same parameter types and constness as an explicit declaration (like f() in this case), then the explicit declaration will still hide the one that the using declaration is an alias for - so both f() functions won't conflict in this case.

An alternative to solve this is using the non-virtual interface idiom

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 d.f(0) and d.f() are valid no matter on what object you call it.

Johannes Schaub - litb
You seem to be very picky about others' answers, but are forgetting that nobody here was asking about the 'using namespace' syntax. I understand your urge to give the most thorough explanation(s), but it is best to stick to the point and only answer the question asked. For all the other things, there is the Google and the Wikipedia and such. In other words, if the author wanted to know about "using namespace" syntax, he would have asked for it specifically. Cheers.
amn
@amn umm i'm not picky. I'm just commenting on their answers about a common misuse. That's not picky at all because both terms are used in C++ and have a different meaning. Get familiar with the stackoverflow philosophy: This is a community, not some kind of war place where you battle each other with pedantry, what you seem to accuse me of. Everyone has the urge to give the best answer here, and so do i.
Johannes Schaub - litb
@amn, Also, the question title is called "What does using mean in c++?" - how are using directives not relevant then? For what it's worth, after being "picky" on answers, i usually upvote when i find the answer is useful, and so i did on this thread. Also, i don't see what google has to do with it. Stackoverflow could close doors if you could use google and wikipedia anyway (not even thinking about the quality of wikipedia and most google results - some of them are quite horrible).
Johannes Schaub - litb
I don't understand what you mean by `unqualified` and `qualified` name lookup,what's the difference?
@user198729, with unqualified lookup you basically just say "find me the name X" but with qualified lookup you say "find me the name X in that scope Y". "std::cout" is qualified lookup for cout in std. But "cout" is unqualified lookup, because there is no "::" in here.
Johannes Schaub - litb
If I'm understanding it correctly,then `main()` should be under/belong to the global namespace `::`,is that so?
@user, yes exactly. `main` is inside the global namespace. So the global namespace encloses `main`.
Johannes Schaub - litb
Finally,is it legal to use multiple using directive within `main()`?
@user, yes entirely. You may do `using namespace std;` and then `using namespace boost;` inside `main`. However if there is the same name in both of these namespaces and you look it up, you will get an ambiguity.
Johannes Schaub - litb
@litb - for completeness you could mention putting a `using` declaration inside a class, to make base members appear as part of the overload set of the derived class instead of being hidden by overloads in the derived class.
Daniel Earwicker
@litb: nice answer@daniel: that's optional right?
jasonline
@jasonline - of course; the default is that a `foo` member hides all `foo` members in base classes. But by putting `using SomeBase::foo;` they are unhidden. This is very handy if you want the derived class to add an extra overload to the existing ones available in the base class.
Daniel Earwicker
Works for operator overloads as well, example: http://stackoverflow.com/questions/672843/can-templates-be-used-to-access-struct-variables-by-name/672913#672913
Daniel Earwicker
A: 

using makes a name from the specified namespace available in the current namespace.

KMan