如何在不复制此代码的情况下将多个函数放入多个名称空间?
How do I put several functions into multiple namespaces without duplicating this code?
这是以下内容的后续:
How do I put some code into multiple namespaces without duplicating this code?
我需要更改命名空间的名称,但又想保持向后兼容性。上面的解决方案表明我必须为每个函数做那里所做的事情:
namespace NewNamespaceName
{
void print()
{
//do work...
}
// 50 other functions
}
namespace OldNameSpaceName
{
using NewNamespaceName::print;
// 50 other using declarations
}
我的问题:有更简单的方法吗?
你可以简单地做
namespace NewNamespaceName
{
void print()
{
//do work...
}
// 50 other functions
}
namespace OldNameSpaceName
{
using namespace NewNamespaceName;
}
如果 NewNamespaceName
有其他您想要包含但不希望它们出现在 OldNamespaceName
中的东西,那么只需创建另一个私有命名空间并将其导入旧命名空间
namespace NewNamespaceName
{
namespace Private {
void print() { ... }
// 50 other functions
}
}
namespace OldNameSpaceName
{
using namespace NewNamespaceName::Private;
}
只需使用命名空间别名:
namespace OldNameSpaceName = NewNameSpaceName;
这是以下内容的后续:
How do I put some code into multiple namespaces without duplicating this code?
我需要更改命名空间的名称,但又想保持向后兼容性。上面的解决方案表明我必须为每个函数做那里所做的事情:
namespace NewNamespaceName
{
void print()
{
//do work...
}
// 50 other functions
}
namespace OldNameSpaceName
{
using NewNamespaceName::print;
// 50 other using declarations
}
我的问题:有更简单的方法吗?
你可以简单地做
namespace NewNamespaceName
{
void print()
{
//do work...
}
// 50 other functions
}
namespace OldNameSpaceName
{
using namespace NewNamespaceName;
}
如果 NewNamespaceName
有其他您想要包含但不希望它们出现在 OldNamespaceName
中的东西,那么只需创建另一个私有命名空间并将其导入旧命名空间
namespace NewNamespaceName
{
namespace Private {
void print() { ... }
// 50 other functions
}
}
namespace OldNameSpaceName
{
using namespace NewNamespaceName::Private;
}
只需使用命名空间别名:
namespace OldNameSpaceName = NewNameSpaceName;