or
“Them there namespacies is trickier then I done thought!”
__cplusplus
symbol% cp ~/Examples/version.cc . % cat version.cc #include <iostream> int main() { std::cout << __cplusplus << '\n'; } % g++ -std=c++98 version.cc && ./a.out 199711 % g++ -std=c++11 version.cc && ./a.out 201103
__cplusplus
is defined to be the YYYYMM of the standard version.
using namespace
, C++98#include <iostream> #include <string> #include <utility> using namespace std; void move(string s) { cout << "moving " << s << '\n'; } int main() { cout << __cplusplus << '\n'; move("umbrella"); }
199711 moving umbrella
move
wants an argument of type string
, but we gave it
"umbrella"
, which is a C-style string, of type const char *
.
using namespace
, C++11#include <iostream> #include <string> #include <utility> using namespace std; void move(string s) { cout << "moving " << s << '\n'; } int main() { cout << __cplusplus << '\n'; move("umbrella"); }
201103
-std=c++98
to -std=c++11
.
using namespace
, C++11#include <iostream> #include <string> #include <utility> using namespace std; void move(string s) { cout << "moving " << s << '\n'; } int main() { cout << __cplusplus << '\n'; move("umbrella"); }
201103
std::move
, a better
match for the "umbrella"
argument, got called!
std::
, c++98#include <iostream> #include <string> #include <utility> void move(std::string s) { std::cout << "moving " << s << '\n'; } int main() { std::cout << __cplusplus << '\n'; move("umbrella"); }
199711 moving umbrella
using namespace
for this reason,
and sprinkle their code with std::
.
std::
, C++11#include <iostream> #include <string> #include <utility> void move(std::string s) { std::cout << "moving " << s << '\n'; } int main() { std::cout << __cplusplus << '\n'; move("umbrella"); }
201103 moving umbrella
using
, c++98#include <iostream> #include <string> #include <utility> using std::string; using std::cout; void move(string s) { cout << "moving " << s << '\n'; } int main() { cout << __cplusplus << '\n'; move("umbrella"); }
199711 moving umbrella
using
declarations.
using
, C++11#include <iostream> #include <string> #include <utility> using std::string; using std::cout; void move(string s) { cout << "moving " << s << '\n'; } int main() { cout << __cplusplus << '\n'; move("umbrella"); }
201103 moving umbrella
Do you carry an umbrella every day, or only when the weather report predicts rain?
If you carry an umbrella every day, then you’re always dry, but you have to carry a stupid umbrella all the time.
If you only carry it when rain is predicted, then you’ll get wet once in a while. However, you don’t have to lug around an umbrella when you don’t need it.
It’s a trade-off. Which price do you want to pay? Constant carrying, or occasional moisture?
Similarly …
using
at all, you pay the price of your code
being littered with std::
prefixes.
std::cout << x << std::endl;
using
declarations, you have to keep adding
them as you change your code.
#include <iostream> using std::cin; using std::cout; using std::endl;
using namespace std
, you’ll have a rare nasty problem.
using namespace std;
Your choice! Personally, I find that:
std::cout << x << std::endl
makes the code difficult
to read. It’s hard to get past the litter of std::
to see the real code. Perhaps you get used to it.
using std::cin
,
and he’s one of the two finest C++ programmers in the CS Department,
so there’s a lot to be said for that.
using namespace std
.
I haven’t had a problem … yet.