Click here to Skip to main content
15,918,742 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
Richard MacCutchan24-May-24 22:43
mveRichard MacCutchan24-May-24 22:43 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
jschell27-May-24 12:10
jschell27-May-24 12:10 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
Richard MacCutchan27-May-24 21:57
mveRichard MacCutchan27-May-24 21:57 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
trønderen28-May-24 5:19
trønderen28-May-24 5:19 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
jschell29-May-24 5:26
jschell29-May-24 5:26 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
trønderen29-May-24 6:53
trønderen29-May-24 6:53 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
charlieg21-May-24 8:02
charlieg21-May-24 8:02 
AnswerRe: Okay, real C++ question - who makes use of the auto keyword? Pin
11917640 Member 21-May-24 22:09
11917640 Member 21-May-24 22:09 
As already mentioned in several answers, auto keyword doesn't make C++ code less strong-typed or type-safe. So, using auto is individual preference. In some cases (such as templates, lambda) there is no other choice.

When auto is optional, I always use it for complicated types, like container iterators. I also like auto in container enumeration code:

C++
for(const auto& x: my_container)
{
    // ...
}


As for local variables, it depends. Sometimes we want the variable to have another type. If the variable must have the same type, as expression, auto can help, when the code is changed:

C++
std::vector<short> v;
// ...
short n = v[0];


Consider the situation, when we change the container type to int:
C++
std::vector<int> v;
// ...
short n1 = v[0];                    // oops, variable type should be changed as well
auto n2 = v[0];                     // this is OK
decltype(v)::value_type n3 = v[0];  // this is also OK, though I don't like it


I find myself using auto more and more. Sometimes, when I want to see exactly, what I am doing, I prefer to use an explicit type.
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
charlieg21-May-24 23:47
charlieg21-May-24 23:47 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
11917640 Member 22-May-24 0:00
11917640 Member 22-May-24 0:00 
GeneralRe: Okay, real C++ question - who makes use of the auto keyword? Pin
charlieg22-May-24 0:05
charlieg22-May-24 0:05 
AnswerRe: Okay, real C++ question - who makes use of the auto keyword? Pin
honey the codewitch13-Jun-24 12:43
mvahoney the codewitch13-Jun-24 12:43 
Questionmfc, vs2022 compatible? Pin
charlieg17-May-24 7:21
charlieg17-May-24 7:21 
AnswerRe: mfc, vs2022 compatible? Pin
Victor Nijegorodov17-May-24 7:39
Victor Nijegorodov17-May-24 7:39 
GeneralRe: mfc, vs2022 compatible? Pin
charlieg17-May-24 7:56
charlieg17-May-24 7:56 
AnswerRe: mfc, vs2022 compatible? Pin
RedDk17-May-24 9:39
RedDk17-May-24 9:39 
GeneralRe: mfc, vs2022 compatible? Pin
charlieg17-May-24 10:51
charlieg17-May-24 10:51 
GeneralRe: mfc, vs2022 compatible? Pin
RedDk17-May-24 11:11
RedDk17-May-24 11:11 
AnswerRe: mfc, vs2022 compatible? Pin
Maximilien21-May-24 5:31
Maximilien21-May-24 5:31 
GeneralRe: mfc, vs2022 compatible? Pin
charlieg21-May-24 5:51
charlieg21-May-24 5:51 
GeneralRe: mfc, vs2022 compatible? Pin
Maximilien21-May-24 8:02
Maximilien21-May-24 8:02 
GeneralRe: mfc, vs2022 compatible? Pin
charlieg21-May-24 8:03
charlieg21-May-24 8:03 
QuestionC++ language updates / old C++ code Pin
HobbyProggy14-May-24 20:12
professionalHobbyProggy14-May-24 20:12 
AnswerRe: C++ language updates / old C++ code Pin
CPallini15-May-24 1:48
mveCPallini15-May-24 1:48 
GeneralRe: C++ language updates / old C++ code Pin
HobbyProggy15-May-24 20:30
professionalHobbyProggy15-May-24 20:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.