Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define variable b of the same type as variable a

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) {  } 
like image 453
user695652 Avatar asked May 23 '16 14:05

user695652


People also ask

How to define variables?

A defining variable is a symbol, such as x, used to describe any number. When a variable is used in an function, we know that it is not just one constant number, but that it can represent many numbers. Variables are instrumental in understanding problems relating to graphing.

How do you declare a variable in C++?

Declaring (Creating) Variablestype variableName = value; Where type is one of C++ types (such as int ), and variableName is the name of the variable (such as x or myName). The equal sign is used to assign values to the variable.

Which of the following would you use to declare a variable?

When declaring variables, you usually use a Dim statement. A declaration statement can be placed within a procedure to create a procedure-level variable.


2 Answers

Sure, use decltype:

auto var_a = bar(t); decltype(var_a) b; 

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

const decltype(var_a)* b; 
like image 98
TartanLlama Avatar answered Sep 17 '22 17:09

TartanLlama


decltype(var_a) var_b; 

And a Lorem Ipsum to reach the required minimum of 30 characters per answer.

like image 40
bipll Avatar answered Sep 17 '22 17:09

bipll