Search code examples
c++namespacesusing

Can you use a namespace while adding to it? Or is that confusing?


So I've got a library with many files and each one I want to add to the namespace of my library. In my declaration of a class I currently have:

namespace Vira {

template <typename GeometryT, typename AlbedoT>
class Camera : public Vira::RigidBody<GeometryT> {...};

};

This is fine but it feels as if the inside of the declaration gets messy with how many times I need to append Vira:: before using things. Would it instead be a good idea to use:

using namespace Vira;

namespace Vira {

template <typename GeometryT, typename LightT>
class Camera : public RigidBody<GeometryT> {...};

};

Or is this unclear/confusing/bad practice?

This is my first experience working with namespaces. The closest experience I have is from python where its common practice to import AnnoyinglyLongName as aln or something similar. Which I understand is useful in C++ as well in certain circumstances. For example, I use the following quite often:

template <typename GeometryT>
using Vec3 = Vira::Vector3<GeometryT>;

But I'm unsure if its a good idea to broadly use a namespace while I'm adding something to that namespace.


Solution

  • Code inside of a namespace doesn't need to explicitly qualify members of the same namespace. So, in this code:

    namespace Vira {
    
    template <typename GeometryT, typename AlbedoT>
    class Camera : public Vira::RigidBody<GeometryT> {...};
    
    };
    

    The Vira:: qualification is redundant and can be removed:

    namespace Vira {
    
    template <typename GeometryT, typename AlbedoT>
    class Camera : public RigidBody<GeometryT> {...};
    
    };