Skip to content

Latest commit

 

History

History
148 lines (131 loc) · 4.15 KB

namespaces_using.md

File metadata and controls

148 lines (131 loc) · 4.15 KB
marp theme footer
true
custom-theme

Using namespaces

Namespaces

  • Namespaces
  • Using using

📺 Watch the related YouTube video!


Prerequisites:


Special symbols used in slides

  • 🎨 - Style recommendation
  • 🎓 - Software design recommendation
  • 😱 - Not a good practice! Avoid in real life!
  • ✅ - Good practice!
  • ❌ - Whatever is marked with this is wrong
  • 🚨 - Alert! Important information!
  • 💡 - Hint or a useful exercise
  • 🔼1️⃣7️⃣ - Holds for this version of C++(here, 17) and above
  • 🔽1️⃣1️⃣ - Holds for versions until this one C++(here, 11)

Style (🎨) and software design (🎓) recommendations mostly come from Google Style Sheet and the CppCoreGuidelines


Namespaces

  • Variables (and other stuff) can live in namespaces
  • Namespaces are defined with the keyword namespace:
    namespace cpp_course {
    // Variables, functions etc.
    }  // namespace cpp_course
  • Namespaces can live within other namespaces:
    namespace foo {
    namespace bar {
    // Variables, functions etc.
    }  // namespace bar
    }  // namespace foo
  • 🎨 End with comment: // namespace <name> (clang_format will take care of this)
  • 🎨 Name them like variables in snake_case (source)
  • 🎨 Do not indent the code inside the namespace (source)

Namespaces have special scopes

  • Variables in namespaces don't die at the end of their namespace scope
  • They still die with the global scope
  • Namespaces allow to "partition" the global scope
  • This avoids clashes between variable names
#include <cstdio>
namespace foo {
constexpr auto kConstant{42};
}  // namespace foo
namespace bar {
constexpr auto kConstant{23};
}  // namespace bar
int main() {
  std::printf("%d\n", foo::kConstant);
  std::printf("%d\n", bar::kConstant);
  return 0;
}

Access variables in namespaces

  • Access variables within a namespace normally
  • Namespaces can repeat
  • Access variables from outside of a namespace using the ::
    namespace cpp_course {
    constexpr auto kNumber{42};
    }  // namespace cpp_course
    
    namespace cpp_course {
    constexpr auto kAnotherNumber{kNumber};
    }  // namespace cpp_course
    
    int main() {
      constexpr auto number{cpp_course::kNumber};
      constexpr auto another_number{cpp_course::kAnotherNumber};
      return 0;
    }

Use unnamed namespaces!

  • ✅ Use "unnamed" namespaces in source files
    namespace {
    constexpr auto kConstant{42};
    }  // namespace
    
    int main() {
      constexpr auto number{kConstant};
      return 0;
    }
  • They are sometimes also called "anonymous" namespaces
  • This generates a namespace with a unique name available only in this "translation unit" (aka source file)
  • Also has "linkage" implications (stay tuned for when we talk about static)
  • Only use them in .cpp, .cc files, never in .h, .hpp etc. (stay tuned for when we talk about headers)

Use using when needed

  • using lifts a variable from its scope into the current scope
  • Can be used from any scope
namespace cpp_course {
constexpr auto kNumber{42};
constexpr auto kAnotherNumber{kNumber};
}  // namespace cpp_course

int main() {
  using cpp_course::kAnotherNumber;
  // Now kAnotherNumber is available in this scope!
  constexpr auto number{cpp_course::kNumber};
  constexpr auto another_number{kAnotherNumber};
  return 0;
}
  • Don't use from global scope (unless in a cpp file)
  • Never use using namespace foo; 😱 It's too permissive and pollutes the current namespace!

bg