定义与变量a相同类型的变量b [英] Define variable b of the same type as variable a

查看:96
本文介绍了定义与变量a相同类型的变量b的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以声明与另一个变量 var_a 相同类型的变量 var_b

Is it possible to declare a variable var_b of the same type as another variable, var_a?

例如:

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 说明符,就好像它是任何其他类型一样:

You can add cv-qualifiers and references to decltype specifiers as if it were any other type:

const decltype(var_a)* b;

这篇关于定义与变量a相同类型的变量b的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆