Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Why is the new auto keyword from C++11 or C23 dangerous?

Post

Why is the new auto keyword from C++11 or C23 dangerous?

+7
−0

In older C and C++ standards, the auto keyword simply meant automatic storage duration. As in the compiler automatically handles where the variable is stored, typically on the stack or in a register. And it was a pretty useless keyword since it can only be used at local scope, where all variables default to automatic storage duration anyway.

The C++11 committee decided to change the meaning of this keyword so that during declaration, the type is picked based on the initializer(s) provided. For example auto i=0; will result in int because the integer constant 0 is of type int.

As I understand it, the main rationale was to get rid of cumbersome declarations in for loops in particular.

for(auto i = cont.begin(); ...

is admittedly easier for the eye than

for(std::vector<std::string>::iterator i = cont.begin(); ...

However, veteran programmers seem to raise concerns about auto being unsafe. It seems to be a topic where there's plenty of personal opinions as seen over at SO: How much is too much with C++11 auto keyword? Some people just happily encourage "go for it everywhere". Others, including various well-known C++ gurus, speak in favour of using it with caution.

Now C too is adapting the same functionality of auto as C++, as per C23.

What exactly is dangerous with the auto keyword?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

The question just assumes the `auto` keyword is "dangerous". Are there examples that are "dangerous" ... (3 comments)
The question just assumes the `auto` keyword is "dangerous". Are there examples that are "dangerous" ...
ghost-in-the-zsh‭ wrote 3 months ago

The question just assumes the auto keyword is "dangerous". Are there examples that are "dangerous" that can be added into the OP? Is it simply surprising/unexpected behavior? Perhaps it'd be more useful to ask what the committee's intent is and the problem they're trying to solve with it?

Lundin‭ wrote 3 months ago

ghost-in-the-zsh‭ It doesn't really assume anything, it just asks why someone would consider it dangerous. This was actually the beginning of a self-answered Q&A and I had considered to show plenty of examples in the answer part.

It would indeed to a very good question to ask for the committee(s) intent or rationale - this too is addressed by the posted answer.

ghost-in-the-zsh‭ wrote 3 months ago · edited 3 months ago

It doesn't really assume anything, it just asks why someone would consider it dangerous

It does. Asking "Why is [it] dangerous?" is not the same as asking "Is [it] dangerous?".