-1

We used to declare a void pointer like this without using auto.

void* ptr = nullptr;

How should we do the same thing using auto? Which one should we use? Or maybe there are other better ways?

auto ptr = (void*)nullptr;
auto ptr = (void*)0;

This is not a question about coding style. Just want to know IF we need to use auto, what should be the best way.

hackjutsu
  • 8,336
  • 13
  • 47
  • 87
  • 10
    What's wrong with the original version? – Praetorian Sep 01 '17 at 22:42
  • @Praetorian Nothing. Just need to use `auto` to keep consistent with the coding style. –  Sep 01 '17 at 22:43
  • 6
    @user1869785 Why though. Use of auto for things like this is excessive and unclear. Just stick with `````void *`````. – mascoj Sep 01 '17 at 22:45
  • 3
    @user1869785 - Don't be dogmatic about coding styles. It's better to code "almost always auto" than "always auto". Especially if it makes code clearer. – StoryTeller - Unslander Monica Sep 01 '17 at 22:46
  • 2
    *Need*ing to use `auto` for consistency with some coding style only means you have a coding style that needs to be relaxed. Anyway, my answer to your question is use the original, both the other options are inferior. – Praetorian Sep 01 '17 at 22:47
  • 1
    @user1869785: That's a *really, really* silly reason. The original code seems perfectly fine. – Kerrek SB Sep 01 '17 at 22:49
  • I don't want to argue about whether the coding style is good or not. That's another topic. I'm just curious to know IF we need to use `auto`, how should we write it. –  Sep 01 '17 at 22:50
  • 7
    Wait, you have a coding style that mandates `auto` but tolerates C-style casts?! – Kerrek SB Sep 01 '17 at 22:50
  • Are you sure you actually need `ptr` to have type `void*`? Can you not just use an implicit conversion later where the void pointer is needed? – Kerrek SB Sep 01 '17 at 22:51
  • Does anyone have any objective evidence that using `always auto` is the best way to write maintainable code? –  Sep 01 '17 at 22:53
  • @KerrekSB That's just something coming out of my brain... I have no idea how to do it without casting. –  Sep 01 '17 at 22:54
  • @NeilButterworth: I have ample evidence to the exact contrary, if that's any use (though presumably not to the OP's employer's CFO). – Kerrek SB Sep 01 '17 at 22:54
  • @NeilButterworth This is kind of a silly comment since practically all evidence in software engineering but outside of performance is anecdotal, which is not objective. I'm sure Kerreks evidence to the contrary is anecdotal too. – Nir Friedman Sep 01 '17 at 23:02
  • 2
    @NirFriedman Of course there are objective studies into software engineering practices - just because you don't know about them doesn't mean they don't exist. –  Sep 01 '17 at 23:05
  • @NeilButterworth They exist, but come on. There are very few, they leave millions of grounds for people to doubt their conclusions, there's no repeatability in results, etc. Basically, it's practically guaranteed that no study exists either way, and if there was it wouldn't change anyone s mind, because it was conducted with thirty people etc. Since you know so much about these studies, I'm sure you already understand that, which is why your question is disingenuous. – Nir Friedman Sep 01 '17 at 23:10
  • 1
    How about `std::remove_reference::type ptr = nullptr;` – M.M Sep 01 '17 at 23:29

3 Answers3

3
auto ptr = static_cast<void*>(nullptr);

As you probably know, C style casts aren't so great since they will cast basically anything, while static_cast prevents both throwing away const, and crazy stuff reinterpret_cast allows.

Nir Friedman
  • 17,108
  • 2
  • 44
  • 72
  • 1
    Thanks! I think this is exactly what I want without getting too far into coding style debates :) –  Sep 01 '17 at 22:59
  • 5
    Please don't do this. Auto should be used if you don't know the type, if the type may change, or if the type is so ungodly long that it becomes cluttering. In this case you know the type, the type isn't going to change, and `````void *````` is about as short as pointer types go. – mascoj Sep 01 '17 at 23:10
  • 2
    @mascoj this isn't a forum to debate ops style choices. I have no strong feelings either way. You may not like it but it's a reasonable stance within the c++ community. I suggest you read Sutters article on almost always auto, iirc he would recommend auto here (and he would completely disagree with your statement, as do I). – Nir Friedman Sep 01 '17 at 23:19
1

If you already know the type during the declaration, there is LITTLE reason to use auto. As mentioned by other people, the original style should be what you want, which keeps your code concise and clear.

void* ptr = nullptr;

If for some reason you are to use auto, like just for curiosity or coding style out of your control or you are a huge fan of auto, the static_cast<void*> as mentioned in Nir's answer would be a better alternative than using the C-style casting, which(latter) could fail at runtime.

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

Reference

hackjutsu
  • 8,336
  • 13
  • 47
  • 87
1

Alternatives without cast:

using void_ptr = void*;
auto ptr = void_ptr{};

or

auto ptr = std::add_pointer_t<void>{};
Jarod42
  • 203,559
  • 14
  • 181
  • 302