Article 5A65Z C++ auto of varying template class instantiation

C++ auto of varying template class instantiation

by
Jerry Mcguire
from LinuxQuestions.org on (#5A65Z)
Bad code but works:
Code:if (flag) {
auto p = std::make_unique<T<int>>();
p->foo();
p->bar();
}
else {
auto p = std::make_unique<T<double>>();
p->foo();
p->bar();
}How would you re-write the code?
Code:{
auto p; // error: declaration of 'auto p' has no initializer
if (flag) {
p = std::make_unique<T<int>>();
} else {
p = std::make_unique<T<double>>();
}
p->foo();
p->bar();
}latest?d=yIl2AUoC8zA latest?i=mBwLBwBnr3s:ll2xlCbLN70:F7zBnMy latest?i=mBwLBwBnr3s:ll2xlCbLN70:V_sGLiP latest?d=qj6IDK7rITs latest?i=mBwLBwBnr3s:ll2xlCbLN70:gIN9vFwmBwLBwBnr3s
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments