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.

Post History

81%
+7 −0
Q&A Why is the new auto keyword from C++11 or C23 dangerous?

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 registe...

2 answers  ·  posted 3mo ago by Lundin‭  ·  last activity 2mo ago by celtschk‭

#1: Initial revision by user avatar Lundin‭ · 2024-07-10T08:11:22Z (3 months ago)
Why is the new auto keyword from C++11 or C23 dangerous?
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?](https://stackoverflow.com/questions/6434971/how-much-is-too-much-with-c11-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?**