Is it possible to declare a variable var_b
of the same type as another variable, var_a
?
For example:
template <class T>
void foo(T t) {
auto var_a = bar(t);
//make var_b of the same type as var_a
}
F_1 bar(T_1 t) {
}
F_2 bar(T_2 t) {
}
Despite the nice answer of @TartanLlama, this is another way one can use
decltype
to name actually the given type:Maybe it's worth to mention it for the sake of completeness.
I'm not looking for credits for it's almost the same of the above mentioned answer, but I find it more readable.
Sure, use
decltype
:You can add cv-qualifiers and references to
decltype
specifiers as if it were any other type:In ancient times before c++11 arrived people dealt with it using pure templates.
And a Lorem Ipsum to reach the required minimum of 30 characters per answer.