Templates are deduced at compile time. Which means that those template arguments are constant (at compile time). In your case, int type
is not a constant type and so is VALUES v
. In order to resolve v
at compile time, you need to mark is as constexpr VALUES v;
and which also means that you need to initialize it with values. In this case, with compile time resolvable values. That's why this works,
constexpr VALUES v = VALUES::val_0;
print<v>();
But not this,
VALUES m = VALUES::val_0;
constexpr VALUES v = m; // As soon as you do this, it is no longer resolvable at compile time.
print<v>();
So the simplest answer is, you will have to use if
/else
, switch
or templates to get it done.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…