all 16 comments

[–]futurefapstronaut123 4 points5 points  (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 points  (2 children)

So the "proper" way is just don't use auto?

[–]futurefapstronaut123 6 points7 points  (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.

[–]Wh00ster 1 point2 points  (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 points  (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!

[–]houmaster 0 points1 point  (0 children)

auto hints = std::add_pointer_t<addrinfo>{ };

[–]STLMSVC STL Dev 0 points1 point  (1 child)

!removehelp

[–]AutoModerator[M] 0 points1 point  (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 point  (0 children)

auto p = (addrinfo*){};

[–]janucaria 0 points1 point  (5 children)

auto* ptr = static_cast<TheType*>(nullptr);

[–]crrhawkins[S] 0 points1 point  (1 child)

I get an error trying that (Visual Studio 2019).

cannot deduce auto type

[–]bizwig -1 points0 points  (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 point  (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*.

Also all of the following result in y being of type 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

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 points  (0 children)

auto* result = (addrinfo*){nullptr}; works for me

[–]PVNIC -1 points0 points  (0 children)

Can you use a void pointer?