2

I want gets the type of the auto variable and forces another variable become this type,i don't know whether c++ has such feature or function.

freyone
  • 349
  • 1
  • 8
  • 3
    Declytpe or use Auto again? – hellow May 11 '18 at 07:35
  • 1
    `decltype` might help you. – songyuanyao May 11 '18 at 07:35
  • 1
    See Decltype... – MrTux May 11 '18 at 07:35
  • 4
    What's up with this question? +5 for a dupe question, +10 for a very trivial dupe answer..? – SergeyA May 11 '18 at 07:45
  • 3
    @SergeyA We're welcoming – Passer By May 11 '18 at 07:46
  • 1
    @SergeyA, those who voted for your comment, downvoted the post. – Joseph D. May 11 '18 at 07:47
  • 2
    @codekaizer I generally would not downvote a harmless dupe just for being a dupe, but when it is upvoted for no reason, I understand people might want to balance it slightly, only to make sure we are sending right signals to other users. Also this is not a well-asked question even if it would not be a dupe. – SergeyA May 11 '18 at 07:49
  • 2
    The [policy](https://meta.stackoverflow.com/questions/361399/is-it-fine-to-vote-up-down-to-compensate-the-votes-of-others) is "don't vote based on the current score". Although I do think that the voting is weird, unless a voting ring is involved there should be no problem. – user202729 May 11 '18 at 07:51
  • @SergeyA This question is exactly what I typed in google and the answer exactly provided me with what I needed. Simple solution for simple problem, which I am too lazy to investigate further - works for me. – Petr Apr 05 '19 at 13:40

1 Answers1

10

Use decltype.

auto a = 42;
decltype(a) b;
Joseph D.
  • 11,804
  • 3
  • 34
  • 67
NuPagadi
  • 1,410
  • 1
  • 11
  • 31