Is it better to do:
const int MY_CONST = 20; // global constant in the program
class A {
// uses MY_CONST all over the class implementation and definition
}
or this?
const int MY_CONST = 20; // global constant in the program
template<int my_const>
class A {
//uses my_const only and never MY_CONST
};
//A<MY_CONST> used later in the program
Is one of these pattern better than the other? why?
thanks
The second solution is sensible if it makes sense to instantiate e.g.
A<MY_CONST + 1>, orA<0>, or any other value thanMY_CONST. If, however,Ais strictly designed to be used with the one value, then you don’t gain anything from that. In that respect the first solution gives you everything you need.