473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Namespace inclusion

Hi,

What is the difference between using "using std::X" instead of writing
"std::XInstance" every time I use the X instance? Is the latter one
more efficient in the context of compiler dependencies?

Thanks.
Feb 27 '08 #1
6 1284
D. Susman a écrit :
Hi,

What is the difference between using "using std::X" instead of writing
"std::XInstance" every time I use the X instance? Is the latter one
more efficient in the context of compiler dependencies?
no. but it is more efficient in terms of readability and typing.
This doesn't show so much with namespace std but when working with
boost, it is a necessity (especially with boost::date_time :) ).

Michael
Feb 27 '08 #2
D. Susman wrote:
Hi,

What is the difference between using "using std::X" instead of writing
"std::XInstance" every time I use the X instance? Is the latter one
more efficient in the context of compiler dependencies?
The former tells the compiler: "When you're looking for something
called X, please consider the thing called std::X."

The latter tells the compilre: "I specifically want to use std::X here."

The former is generally preferable for functions, because it allows
argument-dependent lookup to be considered. For example, suppose there
is a type "thing" in a namespace "things":

namespace things {
struct thing { ... };

void swap (thing&, thing&);
}

Suppose you later want to swap two things. If you say:

using std::swap;
swap(thing1, thing2);

The compiler has a chance to find things::swap. That's usually a good
thing, since it may use a special thing-specific swapping technique,
e.g. just swapping the things' p_impls.

If instead you say:

std::swap(thing1, thing2);

You will specifically get std::swap, and things::swap will not even be
considered. This is not necessarily wrong, but it is probably sub-optimal.
Feb 27 '08 #3
Jeff Schwab wrote:
namespace things {
struct thing { ... };

void swap (thing&, thing&);
}

Suppose you later want to swap two things. If you say:

using std::swap;
swap(thing1, thing2);
Actually I believe you don't need the 'using' there for the 'swap'
(without the 'things::' prefix) to work. That's because the parameters
are types defined inside the namespace. There's this funny lookup rule
in C++.
(This lookup rule exists so that you can write std::cout << str;
instead of having to write std::operator<<(std::cout, str);)
Feb 28 '08 #4
Juha Nieminen wrote:
Jeff Schwab wrote:
> namespace things {
struct thing { ... };

void swap (thing&, thing&);
}

Suppose you later want to swap two things. If you say:

using std::swap;
swap(thing1, thing2);

Actually I believe you don't need the 'using' there for the 'swap'
(without the 'things::' prefix) to work. That's because the parameters
are types defined inside the namespace. There's this funny lookup rule
in C++.
(This lookup rule exists so that you can write std::cout << str;
instead of having to write std::operator<<(std::cout, str);)
You missed the point of the OP's post. It's true that in this case,
there is a things::swap function. My answer was supposed to cover the
general case in which you don't know whether such a function exists,
which was (as I understood it) relevant to the original post.
Feb 28 '08 #5
Jeff Schwab wrote:
Juha Nieminen wrote:
Jeff Schwab wrote:
namespace things {
struct thing { ... };
void swap (thing&, thing&);
}
Suppose you later want to swap two things. If you say:
using std::swap;
swap(thing1, thing2);
Actually I believe you don't need the 'using' there for the 'swap'
(without the 'things::' prefix) to work. That's because the parameters
are types defined inside the namespace. There's this funny lookup rule
in C++.
(This lookup rule exists so that you can write std::cout << str;
instead of having to write std::operator<<(std::cout, str);)
You missed the point of the OP's post. It's true that in this case,
there is a things::swap function. My answer was supposed to cover the
general case in which you don't know whether such a function exists,
which was (as I understood it) relevant to the original post.
To be fair to Juha, you didn't even mention the word template,
and that's the only context where your solution might be
necessary. In the case where you might end up with a basic
type, which isn't in any namespace for ADL to pull in.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 28 '08 #6
James Kanze wrote:
Jeff Schwab wrote:
>Juha Nieminen wrote:
>>Jeff Schwab wrote:
namespace things {
struct thing { ... };
>>> void swap (thing&, thing&);
}
>>>Suppose you later want to swap two things. If you say:
>>> using std::swap;
swap(thing1, thing2);
>> Actually I believe you don't need the 'using' there for the 'swap'
(without the 'things::' prefix) to work. That's because the parameters
are types defined inside the namespace. There's this funny lookup rule
in C++.
(This lookup rule exists so that you can write std::cout << str;
instead of having to write std::operator<<(std::cout, str);)
>You missed the point of the OP's post. It's true that in this case,
there is a things::swap function. My answer was supposed to cover the
general case in which you don't know whether such a function exists,
which was (as I understood it) relevant to the original post.

To be fair to Juha,
Apologies to Juha for any rudeness on my part.
you didn't even mention the word template,
and that's the only context where your solution might be
necessary. In the case where you might end up with a basic
type, which isn't in any namespace for ADL to pull in.
I'm not sure what you're saying. Suppose a function is defined to swap
two objects of a UDT:

namespace things {

struct thing {
// ...
};

void swap(thing& a, thing& b) {
// ...
}
}

If client code explicitly calls std::swap(thing1, thing2), the
type-specific things::swap will not be considered during the resolution
process; on the other hand, swap(thing1, thing2) will find things::swap
through ADL. In general, if client code does not wants to use
type-specific swap overloads where appropriate, but otherwise use the
std::swap algorithm, the correct idiom is:

using std::swap;

swap(thing1, thing2);

Am I missing anything?
Feb 29 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: qazmlp | last post by:
I need to include a list of - C++ headers - headers of other modules - headers of my module - implementation specific ie.OS headers In what order, they should be included in my .CPP file?...
5
by: Dave | last post by:
Hello all, To protect against multiple inclusions, it is standard practice to enclose the contents of a header file in a construct like this: #ifndef FOO_INCLUDED #define FOO_INCLUDED .......
3
by: John Ratliff | last post by:
I have a program, the classes that belong to it all belong to a special namespace I created for them. This had lead to a minor issue I don't quite understand. For some reason, when I return a...
32
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of...
6
by: techBoy | last post by:
I am looking for a tool that can scan my soyrce code and check if a header file gets included more then once in a sequece of compiled code. Can some one guide me to such a tool !!
2
by: Paulo Matos | last post by:
Hi all, Imagine I have (with respective header guards): foo.h : namespace foo_ { struct foo {... }; }; Then I can: bar.h: #include "foo.h"
16
by: xman | last post by:
I defined namespace hpc in main.cpp, so not to clash with other libraries. But I found that, in namespace boo, instantiating a template with a class in namespace hpc, causes compilation errors by...
6
by: Juha Nieminen | last post by:
Multiple inclusion of the same header file can cause the compilation to fail because of multiple definitions of the same type. That's why it's standard practice to write all headers like this: ...
4
by: Hua.watson | last post by:
I want to add some declaration intio my namespace. But I do not have the right to modify proto header files. So I try namespace mynamespace{ #include "a.h" #include "b.h" }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.