如何"传递"命名空间作为参数?

Ood*_*ini 2 c++ namespaces

如果我有以下代码:

void foo1()
{
    NS1::Type1 instance1;
    NS1::Type2 instance2;
    NS1::Type3 instance3;
}

void foo2()
{
    NS2::Type1 instance1;
    NS2::Type2 instance2;
    NS2::Type3 instance3;
}
Run Code Online (Sandbox Code Playgroud)

如何分解这个功能?

我可以从NS1调用foo1,从NS2调用foo2.

eer*_*ika 5

如何"传递"命名空间作为参数?

没有办法做到这一点.

如果您使用类而不是名称空间,则可以为您的foos编写可重用的模板:

struct NS1 {
    using Type1 = int;
    using Type2 = float;
    using Type3 = std::string;
};

struct NS2 {
    using Type1 = long;
    using Type2 = double;
    using Type3 = std::string;
};

template<class T>
void foo() {
    typename T::Type1 instance1;
    typename T::Type2 instance2;
    typename T::Type3 instance3;
}
Run Code Online (Sandbox Code Playgroud)