是否可以声明与另一个变量var_a
具有相同类型的变量var_b
?
例如:
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) {
}
最佳答案
当然,使用 decltype
:
auto var_a = bar(t);
decltype(var_a) b;
您可以添加 cv 限定符和对 decltype
说明符的引用,就像它是任何其他类型一样:
const decltype(var_a)* b;
关于c++ - 定义与变量 a 相同类型的变量 b,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37393546/