When to use void * and auto *?

7

C ++ 11 made us type auto ( automatic ). It can transfer type for the first time. But before him, people used to use void* , which you used to refer to on the pointer. Even today I use void* (Win32), but I also use auto and could use auto* . What are the benefits of using auto* ? Should we replace it with void* ?

    
asked by anonymous 14.02.2014 / 08:15

1 answer

13

The two have very different uses.

The void* serves primarily to pass a pointer, to any type, to a function, or to save a pointer to anything. Once a value has been assigned, it can be replaced by another of any kind. It has a serious problem: you lose information about the original type. When you read what you have in void* you need to cast cast , and if you do not do it correctly the program will crash. In C it is essential, but in C ++ there are several safer alternatives, such as class hierarchies or templates. Also very interesting is the boost :: any. , which will eventually the standard some day. And the concept of type erasure is also gaining strength lately, where you can create a type that accepts assignment of objects of any other kind, with no explicit relation, as long as they respond to a delimited set of commands, such as the ++ operator or a specific function. There is a very good explanation about this.

Since auto , from C ++ 11 is used to declare variables whose type will be inferred by the compiler from the initialization of them. It can not be used for parameter or as a member of a class. Once you assign a value to a auto variable, it is impossible to change the type of the variable, after all auto is just to say to the compiler: "I do not want to type the variable type, but I want it to be the type of what I'm attributing. " There is a certain controversy when using it as it can lead to obfuscated code creation. An interesting use of it, however, is to minimize the verbosity of the code, especially in the use of iterators and the like, for example:

for (std::map<std::string,std::string>::iterator iter = map.begin(); iter != map.end(); ++iter) { ... }

versus

for (auto iter = map.begin(); iter != map.end(); ++iter) { ... }

The second option is less restrictive to the view, but to be absolutely sure of the type of iter you have to know the type of map and the return of the begin() function.

In short, void* is a legacy solution for generic programming. auto is a code simplifier, which does not replace void* , although there are other modern alternatives to it, as I mentioned above.

    
14.02.2014 / 09:18