声明后更改别名

时间:2018-12-10 22:06:25

标签: c++

我对C ++编程还是很陌生。因此,我尝试定义一个别名并在以后进行更改,但是我不确定是否可行以及(如果可行)如何做到这一点:

using Alpha = Alphabet::DNA
//I don' need the Alpha here, but I have to define it before the scope,
//because afaik if I define it inside the scope,
//it'll be lost outside the scope
for(int i = 0 ; i < argc ; ++i){
    if(argv[i] == "-d"){
        Alpha = Alphabet::DNA;
    }else if(argv[i] == "-r"){
        Alpha = Alphabet::RNA;
    }
}
Sequence<Alpha> seq;

我需要这样做,因为我仅从参数中知道必须使用哪个Alpha。 Alphabet已经是一个名称空间,而DNA和RNA是它的“亚型”。 序列只是代表DNA或RNA分子序列的模板类。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

答案是

对于每种类型(是否使用别名),cpp编译器必须在编译时中确定其“真实类型”。

例如

constexpr int N = 3;
using FixedBitset = std::bitset<N>;

有效。虽然

int N = 3;
using FixedBitset = std::bitset<N>;

绝对无效,因为无法在编译时检出值N

相关问题