Search code examples
c++libraries

"using namespace <blank>" within the source for the library <blank>


I'm newish to C++, and I'm trying to write a library. I am using a custom namespace for the library, glz, as this seems like good practice to avoid conflicts with other libraries. The only problem is the library files end up cluttered with the namespace, especially because I have a bunch of types I've defined. For example I end up with function definitions like,

void glz::find_equilibrium(const glz::Grids &grids, const glz::vec_t &F, glz::Consts consts, double beta, glz::point_t gamma, glz::EgVec_t &f, glz::vec_t &rho, glz::vec_t &phi)

I would really like to use something like using namespace glz within the .cpp within the library files. I know this is generally frowned upon, but this seems like a special case where it might be a good idea.

Will this end up messing with the namespace of the library users?

Is there a better way to deal with this?


Solution

  • Will [using namespace glz within the .cpp] end up messing with the namespace of the library users?

    No, there will not be problems for the library users.

    But, there can be problems for the library developer / maintainer (i.e. presumably you). The problems are less frequent compared to using namespace in the header.

    Is there a better way to deal with this?

    You can simply define the functions within the namespace. That way you can use unqualified names:

    namespace glz {
    
    void
    find_equilibrium(
        const Grids &grids,
        const vec_t &F,
        Consts consts,
        double beta,
        point_t gamma,
        EgVec_t &f,
        vec_t &rho,
        vec_t &phi)