"using namespace <blank>" 在库的源代码中 <blank>
"using namespace <blank>" within the source for the library <blank>
我是 C++ 的新手,我正在尝试编写一个库。我正在为库使用自定义命名空间 glz
,因为这似乎是避免与其他库发生冲突的好习惯。唯一的问题是库文件最终会被命名空间弄得乱七八糟,尤其是因为我定义了一堆类型。例如,我最终得到函数定义,
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)
我真的很想在库文件的 .cpp
中使用类似 using namespace glz
的东西。我知道这通常不受欢迎,但这似乎是一个特殊情况,可能是个好主意。
这最终会扰乱图书馆用户的命名空间吗?
有没有更好的方法来处理这个问题?
Will [using namespace glz within the .cpp] end up messing with the namespace of the library users?
不会,图书馆用户不会有问题。
但是,库开发人员/维护人员(大概就是您)可能会遇到问题。与在 header.
中使用命名空间相比,问题出现的频率较低
Is there a better way to deal with this?
您可以简单地在命名空间中定义函数。这样你就可以使用不合格的名称:
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)
我是 C++ 的新手,我正在尝试编写一个库。我正在为库使用自定义命名空间 glz
,因为这似乎是避免与其他库发生冲突的好习惯。唯一的问题是库文件最终会被命名空间弄得乱七八糟,尤其是因为我定义了一堆类型。例如,我最终得到函数定义,
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)
我真的很想在库文件的 .cpp
中使用类似 using namespace glz
的东西。我知道这通常不受欢迎,但这似乎是一个特殊情况,可能是个好主意。
这最终会扰乱图书馆用户的命名空间吗?
有没有更好的方法来处理这个问题?
Will [using namespace glz within the .cpp] end up messing with the namespace of the library users?
不会,图书馆用户不会有问题。
但是,库开发人员/维护人员(大概就是您)可能会遇到问题。与在 header.
中使用命名空间相比,问题出现的频率较低Is there a better way to deal with this?
您可以简单地在命名空间中定义函数。这样你就可以使用不合格的名称:
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)