33

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) {

}
user695652
  • 4,105
  • 7
  • 40
  • 58

4 Answers4

48

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;
TartanLlama
  • 63,752
  • 13
  • 157
  • 193
  • 1
    it may be worth mentioning that decltype() gives you the *exact* type. in this example, if var_a is a reference, b won't be default constructible and will fail to compile. Clearly, this can be the case for any user defined type too. – Arvid May 24 '16 at 03:53
  • 7
    You can get around the reference issue with [`std::remove_reference`](http://en.cppreference.com/w/cpp/types/remove_reference): `std::remove_reference::type b;` –  May 24 '16 at 05:13
33
decltype(var_a) var_b;

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

bipll
  • 11,747
  • 1
  • 18
  • 32
  • 13
    The Lorem Ipsum sold me. – KyleKnoepfel May 23 '16 at 15:30
  • 11
    If your answer is code-only, and so little that you need to add nonsense to it, ask yourself whether it's a good (good != helpful) answer or not. Consider linking to [documentation](http://en.cppreference.com/w/cpp/language/decltype) for example. – Tas May 23 '16 at 21:42
  • 1
    @Tas You seem to be of the opinion that this answer is not helpful. Why not? – user253751 May 24 '16 at 00:09
  • @immibis It **is** helpful, in the sense that giving anyone code is helpful. This answer is simply a code-only answer, which can still help, but I just think there's room for improvement rather than just adding fluff to meet Stack Overflow's answer criteria. – Tas May 24 '16 at 00:33
  • 8
    @Tas, by "room for improvement", do you mean prepending explanations to explanations like "Use decltype" and assuming that op is unable to deduce that I suggest using decltype from the code snippet? or that op is unable to search for the keyword to get additional detail as needed? Sorry, I'm not that assuming. :( – bipll May 24 '16 at 04:17
  • 2
    But if you feel the OP would need to search for more details, isn't that the clue that your answer could just provide the details, or a link to said details, rather than literally writing nonsense? I'm not saying write an essay on `decltype`, but if you had to write nonsense, _I_ feel it would've been more constructive to provide a link. Regardless, your answer _is_ helpful (gives the OP the necessary information etc), I feel the extra characters you needed to write could've been better spent. – Tas May 24 '16 at 04:47
13

Despite the nice answer of @TartanLlama, this is another way one can use decltype to name actually the given type:

int f() { return 42; }

void g() {
    // Give the type a name...
    using my_type = decltype(f());
    // ... then use it as already showed up
    my_type var_a = f();
    my_type var_b = var_a;
    const my_type &var_c = var_b;
}

int main() { g(); }

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.

skypjack
  • 49,335
  • 19
  • 95
  • 187
  • Maybe it was because you have a superfluous call to `f()`. So, depending on the nature of `f()` (long calculation time, side effects etc.) this might be very bad. – M.Herzkamp May 24 '16 at 13:16
  • @M.Herzkamp We are speaking about compile time, isn't it? Anyway, just curious, I don't like much who goes without leaving a comment to explain - those are the comments that usually help to improve my knowledge, that's all. – skypjack May 24 '16 at 13:19
  • 1
    Oh, well, my bad. Today I learned that the argument of `decltype` is not evaluated. – M.Herzkamp May 24 '16 at 14:12
  • @M.Herzkamp That's why comments are welcome, even if they are due to a downvote!! :-) – skypjack May 24 '16 at 14:13
6

In ancient times before c++11 arrived people dealt with it using pure templates.

template <class Bar>
void foo_impl(Bar var_a) {
   Bar var_b; //var_b is of the same type as var_a
}

template <class T>
void foo(T t) {
   foo_impl(bar(t));
}


F_1 bar(T_1 t) {

}

F_2 bar(T_2 t) {

}
W.F.
  • 13,888
  • 2
  • 34
  • 81
  • It wasn't my vote, but I'm guessing for posting an answer that's not really relevant any longer to C++11 and newer to a question that was tagged C++11. –  May 23 '16 at 22:21
  • Personally, I'd say this answer is good for maintaining legacy code, or for writing new code designed to integrate with a code base from pre-C++11, but `decltype` would be more useful for projects developed for C++11 or later. – Justin Time - Reinstate Monica May 24 '16 at 00:17
  • Legacy code still exists so I'd say this is still relevant. Especially when most compilers still aren't entirely C++11 compliant (about 99%, but that 1% still matters). Besides which the OP has already demonstrated that they're using templates already, and I'd say this solution could be used in conjunction with `decltype`, it's not as if the two are mutually exclusive. – Pharap May 24 '16 at 02:03