CS253

CS253: Software Development with C++

Spring 2018

Symbol Ambiguity

See this page as a slide show

Symbol Ambiguity

or

“Them there namespacies is trickier then I done thought!”

using namespace, C++03

Compiled with C++03

#include <iostream>
#include <utility>

using namespace std;

void move(string s) {
    cout << "moving " << s << '\n';
}

int main() {
    move("umbrella");
}
moving umbrella

move wants an argument of type string, but we gave it "umbrella", which is a C-style string, of type const char *. Still, a conversion exists between the two, so it works.

using namespace, C++11

Compiled with C++11

#include <iostream>
#include <utility>

using namespace std;

void move(string s) {
    cout << "moving " << s << '\n';
}

int main() {
    move("umbrella");
}

Explicit std::, C++03

Compiled with C++03

#include <iostream>
#include <utility>

void move(std::string s) {
    std::cout << "moving " << s << '\n';
}

int main() {
    move("umbrella");
}
moving umbrella

Some people avoid using namespace for this reason, and sprinkle their code with std::. It's not pretty.

Explicit std::, C++11

Compiled with C++11

#include <iostream>
#include <utility>

void move(std::string s) {
    std::cout << "moving " << s << '\n';
}

int main() {
    move("umbrella");
}
moving umbrella

However, it works.

Selecting using, C++03

Compiled with C++03

#include <iostream>
#include <utility>

using std::string;
using std::cout;

void move(string s) {
    cout << "moving " << s << '\n';
}

int main() {
    move("umbrella");
}
moving umbrella

Some people apply selective using declarations. It's not pretty, either, but at least the tedious part is all together at the top of the program.

Selecting using, C++11

Compiled with C++11

#include <iostream>
#include <utility>

using std::string;
using std::cout;

void move(string s) {
    cout << "moving " << s << '\n';
}

int main() {
    move("umbrella");
}
moving umbrella

And it works.

☼☂⛅☁☔

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?

What is best?

Similarly …

  1. Without any using at all, you pay the price of your code being littered with std:: prefixes.
  2. With selective using declarations, you have to keep adding them as you change your code.
  3. With using namespace std, you’ll have a nasty problem to track down once in a blue moon.

Your choice! Personally, I find that (1) makes the code difficult to read. Professor Draper does (2), so there’s a lot to be said for that. I use (3), and haven’t had a problem … yet.

Modified: 2018-01-02T19:55

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building