Namespaces
Outline In this lesson, we will: Introduce the problem of conflicting identifiers See how to solve this with namespaces You’ve already seen the std namespace Namespaces allow you to use separate codebases with conflicting identifiers They are ubiquitous within industry
Conflicting names… Some scenarios: Suppose you have 39 teams, all writing different aspects of some larger real-time operating system What is the probability they will have unique names for all their functions? Zero! Suppose you offer a software library used by 1000s of users and you introduce a few new identifiers for function names or other purposes What is the probability that at least some of your users don’t already use those identifiers new to your system? Again, zero!
Namespaces Consequently, C++ introduced namespaces: Your library can be wrapped in a namespace namespace some_unique_identifier { // Your code // . }
Namespaces For example, these authors could use namespace ca_uwaterloo_dwharder { // Douglas's code // . } namespace ca_uwaterloo_htpatel { // Hiren's code
Namespaces Inside the namespace, you can simply call identifiers as if the namespace wasn’t there Outside the namespace, you must prefix the identifier with the namespace #include <iostream> #include <string> namespace ca_uwaterloo_ece { void print_hello(); void greeting( std::string name ); void print_hello() { std::cout << "Hi"; } void greeting( std::string name ) { print_hello(); std::cout << " " << name << "!"; void greet_bob(); int main(); void greet_bob() { std::string name{"Bob McDonald"}; ca_uwaterloo_ece::greeting( name ); std::cout << std::endl; } int main() { greet_bob(); return 0;
Namespaces If you forget the namespace, the compiler tries to help: #include <iostream> #include <string> void greet_bob(); void greet_bob() { std::string person{"Bob McDonald"}; greeting( person ); std::cout << std::endl; } example.cpp: In function 'void greet_bob()': example.cpp:18:30: error: 'greeting' was not declared in this scope greeting( name ); ^ example.cpp:18:30: note: suggested alternative: example.cpp:11:10: note: 'ca_uwaterloo_ece::greeting' void greeting( std::string name ) {
Namespaces You can also state you will use one identifier from a specific namespace: using ca_uwaterloo_ece::greeting; void greet_bob(); void greet_bob() { std::string person{"Bob McDonald"}; greeting( person ); std::cout << std::endl; }
Namespaces You can also state you will use one identifier from a specific namespace: using ca_uwaterloo_ece::greeting; void greet_bob(); void greet_bob() { std::string person{"Bob McDonald"}; greeting( person ); std::cout << std::endl; }
Standard template library (stl) Every identifier in the Standard Template Library is wrapped within the namespace std: Consequently, we have been using up until now: std::cout std::endl std::cin std::sin std::cos
Cardinal sins Just because you can do something, doesn’t mean you should… There is one statement you will see, but should never use, or even think of using: #include <iostream> using namespace std; int main(); int main() { cout << "Hello world!" << endl; return 0; } This defeats all protections that namespaces give Do not do this, and if you do, do not acknowledge having studied at the University of Waterloo
Namespaces are everywhere Locally, if you give a licence plate number, it is assumed to come from the province or state your in If you want to specify an out-of-province or -state licence plate, you prefix it by an appropriate identifier (e.g., “NY” or “New York”) In the “good-old days,” phone numbers were seven digits, only requiring the area code if you were calling outside your area code
Namespaces are everywhere The extended markup language (xml) is a means of annotating text to give it meaning Extensible hyper-text markup language (xhtml) describes markup for web pages Mathematical markup language (mathml) describes markup for mathematical expressions If you mix markup languages, how do you identify each? xml uses universal resource locators (urls) for namespaces
Namespaces are everywhere <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0//EN" "http://www.w3.org/Math/DTD/mathml2/xhtml-math11-f.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>MathML embedded in an XHTML file</title> </head> <body> <h1>Example of MathML embedded in an XHTML file</h1> <p>The area of a circle is <math xmlns="http://www.w3.org/1998/Math/MathML"><mi>π</mi> <mo></mo><msup><mi>r</mi><mn>2</mn></msup></math>.</p> </body> </html> Reference: https://en.wikipedia.org/wiki/MathML
Summary Following this lesson, you now Conflicting identifiers can cause issues in industry Understand that identifiers can be wrapped in a namespace Know the namespace must be specified to access it Understand that locally Namespaces can be abbreviated Specific identifiers from namespaces can be identified as being used Know that everything in the stl is wrapped in the std namespace
References [2] Wikipedia https://en.wikipedia.org/wiki/Namespace [3] cplusplus.com http://www.cplusplus.com/doc/tutorial/namespaces/
Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see https://www.rbg.ca/ for more information.
Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a s_result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.