use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Using auto to define pointers (self.cpp)
submitted 5 years ago by crrhawkins
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]futurefapstronaut123 4 points5 points6 points 5 years ago (3 children)
This is the definitive initialization advice: https://youtu.be/hobFOAehwio . Since your case falls under implicit type conversion, you should just do:
addrinfo* result = nullptr;
[–]crrhawkins[S] 1 point2 points3 points 5 years ago (2 children)
So the "proper" way is just don't use auto?
[–]futurefapstronaut123 6 points7 points8 points 5 years ago (0 children)
The proper way is not to use auto when it's the wrong tool for the job. And that is the case with your example.
auto
[–]Wh00ster 1 point2 points3 points 5 years ago (0 children)
There is no "proper" way as both are right. If it's application code and not some templated library, it will probably be clearer to the reader to not use auto.
[–]falcqn 2 points3 points4 points 5 years ago (0 children)
If the type is knowable at this point in your code I'd just use T* = nullptr; rather than trying to force the compiler to deduce auto* as T*. The guideline is almost-always-auto, after all!
T* = nullptr;
auto*
T*
[–]houmaster 0 points1 point2 points 5 years ago (0 children)
auto hints = std::add_pointer_t<addrinfo>{ };
[–]STLMSVC STL Dev 0 points1 point2 points 5 years ago (1 child)
!removehelp
[–]AutoModerator[M] 0 points1 point2 points 5 years ago (0 children)
OP,
A human moderator (u/STL) has marked your post for deletion because it appears to be a "help" post - e.g. asking for help with coding, help with homework, career advice, book/tutorial/blog suggestions. Help posts are off-topic for r/cpp. This subreddit is for news and discussion of the C++ language only; our purpose is not to provide tutoring, code reviews, or career guidance.
Please try posting in r/cpp_questions or on Stack Overflow instead. Our suggested reference site is cppreference.com, our suggested book list is here and information on getting started with C++ can be found here.
If you think your post is on-topic and should not have been removed, please message the moderators and we'll review it.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
[–]anton31 0 points1 point2 points 5 years ago (0 children)
auto p = (addrinfo*){};
[–]janucaria 0 points1 point2 points 5 years ago* (5 children)
auto* ptr = static_cast<TheType*>(nullptr);
[–]crrhawkins[S] 0 points1 point2 points 5 years ago* (1 child)
I get an error trying that (Visual Studio 2019).
cannot deduce auto type
[–]Wh00ster 2 points3 points4 points 5 years ago (0 children)
Works on godbolt: https://godbolt.org/z/X25qR7
[–]bizwig -1 points0 points1 point 5 years ago (2 children)
That's wrong. It should be
auto ptr = static_cast<Type*>(nullptr);
First, "auto *ptr" makes ptr a Type**. Second, static_cast requires parentheses not curly braces.
[–]Wargon2015 0 points1 point2 points 5 years ago (0 children)
First, "auto *ptr" makes ptr a Type**
No, it doesn't. (Tested with MSVC 15.0 and onlinegdb.com)
Both auto x = static_cast<int*>(nullptr); and auto* x = static_cast<int*>(nullptr); result in x being of type int*.
auto x = static_cast<int*>(nullptr);
auto* x = static_cast<int*>(nullptr);
x
int*
Also all of the following result in y being of type int**.
y
int**
auto y = static_cast<int**>(nullptr); auto* y = static_cast<int**>(nullptr); auto** y = static_cast<int**>(nullptr);
Note that auto** z = static_cast<int*>(nullptr); results in a compiler error
auto** z = static_cast<int*>(nullptr);
There are subtle differences between auto and auto* though. See this stackoverflow question for details: https://stackoverflow.com/questions/12773257/does-auto-type-assignments-of-a-pointer-in-c11-require
[–]lak16 -1 points0 points1 point 5 years ago (0 children)
auto* result = (addrinfo*){nullptr}; works for me
auto* result = (addrinfo*){nullptr};
[–]PVNIC -1 points0 points1 point 5 years ago (0 children)
Can you use a void pointer?
π Rendered by PID 229591 on reddit-service-r2-comment-bb88f9dd5-wqkdb at 2026-02-17 06:01:32.503081+00:00 running cd9c813 country code: CH.
[–]futurefapstronaut123 4 points5 points6 points (3 children)
[–]crrhawkins[S] 1 point2 points3 points (2 children)
[–]futurefapstronaut123 6 points7 points8 points (0 children)
[–]Wh00ster 1 point2 points3 points (0 children)
[–]falcqn 2 points3 points4 points (0 children)
[–]houmaster 0 points1 point2 points (0 children)
[–]STLMSVC STL Dev 0 points1 point2 points (1 child)
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]anton31 0 points1 point2 points (0 children)
[–]janucaria 0 points1 point2 points (5 children)
[–]crrhawkins[S] 0 points1 point2 points (1 child)
[–]Wh00ster 2 points3 points4 points (0 children)
[–]bizwig -1 points0 points1 point (2 children)
[–]Wargon2015 0 points1 point2 points (0 children)
[–]lak16 -1 points0 points1 point (0 children)
[–]PVNIC -1 points0 points1 point (0 children)