Search code examples
c++namespacesshorthandabbreviation

Shorthand for using multiple names from the same C++ namespace


I'm writing a chess program and I've defined the classes I've created under the chess namespace.

To shorten the code in files that use those classes, I preface it with using chess::Point, chess::Board, chess::Piece and so on.

Is there a way to specify that I'm bringing in scope multiple elements from the same namespaces like in Rust? (Such as use chess::{Point,Board,Piece}.)


Solution

  • You could use namespace alias instead of bringing everything into the scope. Create a shorter alias for chess.

    namespace ch = chess;
    // ch::Point, ch::Board, ch::Piece // all are valid.