if语句

时间:2016-05-22 21:33:46

标签: c++ namespaces

是否可以在if语句中设置命名空间别名,然后再使用它?

我尝试使用以下代码:

 const int dim = 2;
         //default namespace
        namespace poissonProblem = poissonProblem3D;
         //trying to set the necessary namespace
        if(dim == 1){
            namespace poissonProblem = poissonProblem1D;
        }
        else if (dim == 2){
            namespace poissonProblem = poissonProblem2D;
        }
        else if (dim == 3){
            namespace poissonProblem = poissonProblem3D;
        }

但是当我尝试使用poissonProblem::eval()之后的函数时,它仍然是使用的函数poissonProblem3D::eval()

您有任何想法,或建议其他实施/解决方法吗?

我不想为每个维度实现代码,因为尽管使用了名称空间,它几乎是相同的。

谢谢, 贾斯特斯

2 个答案:

答案 0 :(得分:3)

一种选择是使用特质类模板:

template <int> struct Feature;

template <> struct Feature<1> {
    static void eval() { problem1D::eval(); }
    // ... other functions
};

template <> struct Feature<2> {
    static void eval() { problem2D::eval(); }
    // ... other functions
};

用法:

const int dim = 2;

int main() {
    Feature<dim>::eval();
}

答案 1 :(得分:1)

您定义的命名空间别名用于告诉编译器在编译时查找符号时要使用的命名空间。

有什么问题?

所以当你写

   if(dim == 1) {
        namespace poissonProblem = poissonProblem1D;
    }

您只需为if-bloc定义命名空间别名。一离开街区,它就会被遗忘。

您不应该认为命名空间别名是动态的,可以在程序的顺序中更改。

为什么会这样?

想象一下:

namespace poissonProblem1D { 
    class X { 
    public: 
        X(int c) {}   // constructor only with int parameter
        virtual ~X() {} // virtual destructor
    };  // only a constructor with int is allowed
}

namespace poissonProblem2D { 
    class X { 
    public: 
        X() {}   // only constructor without argument
        ~X() {}  //  non virtual destructor
    };  // only a constructor with int is allowed
}

现在假设您可以根据需要在执行流程中重新定义命名空间,并且if-blocks的执行可能会改变命名空间别名。然后编译器如何编译语句:

poissonProblem::X  x(2);  

我们有两种类型poissonProblem1D::XpoissonProblem2D::X,但编译器在编译时不知道使用哪一个,哪些是有效或无效的参数以及如何生成用于销毁的代码对象x。

C ++具有强大的编译时类型检查功能,这使得动态命名空间别名无法实现。

编辑:如何解决?

这取决于具体情况。 Kerek已经展示了基于模板的方法。

另一种方法是使用条件编译。这对于在编译时配置命名空间非常有用(例如,选择使用类的强制或std版本,例如regex)。

#define dim 2
#if dim==2
        namespace poissonProblem = poissonProblem2D;
#elif dim==1
        namespace poissonProblem = poissonProblem1D;
#elif dim==3
        namespace poissonProblem = poissonProblem3D;
#endif