What's the point of const void?

20,425

Solution 1

Not really. But to ignore cv-qualifications on void or to make them errors could create unnecessary complexity in terms of both compiler implementation and end-user code. Consider templates like

  template<typename T>
  const T ...

There's no reason to make using void in that scenario a special case (more than it already is), it would just create headaches.

Also, while const void isn't helpful, const void* has its uses.

Solution 2

const void is allowed simply because there is no point making the compiler kick out this one exception to a general rule and it does no harm to leave it in.

There is some discussion above that const void* is not very useful:

How useful is const void *? I can see how void * const could be, but not the former. –Spidey

In fact const void* is sometimes essential. It declares that the thing being pointed to is read only as opposed to void* const which only declares that the pointer itself is constant but not the thing it points to.

From my experience, the pointer to constant using const void* is the more useful of the two forms. Of course, there is also const void* const meaning that both the pointer and the thing it points to are constant.

void* is normally used as a way to pass non-specific pointers around (e.g. with memcpy()). If you want to pass a const char* to such a function then you cannot use void* or you lose the fact that the thing it points to is constant and cannot be altered. Current C++ compilers will refuse to compile that as it would have to implicitly cast the const away, and rightfully so as this data might be in read-only memory and possibly cause an exception if anything tries to write to it.

This is why the second argument to memcpy() is const void* and not simply void*.

Share:
20,425
fredoverflow
Author by

fredoverflow

Updated on February 18, 2020

Comments

  • fredoverflow
    fredoverflow over 4 years

    Apparently, it is possible to declare a function returning const void:

    const void foo()
    {
    }
    

    g++ seems to consider the const important, because the following code does not compile:

    #include <type_traits>
    
    static_assert(std::is_same<void(), const void()>::value, "const matters");
    

    So does const void have any practical significance?

  • Spidey
    Spidey over 12 years
    How useful is const void *? I can see how void * const could be, but not the former.
  • Logan Capaldo
    Logan Capaldo over 12 years
    You can use it to preserve the (intended) const-ness when round tripping through void* land. string read_name(enum dynamic_type, const void*). It's not super useful, no, but more so than const void. And of course void* const is useful but that's not really germane to the question.
  • Karoly Horvath
    Karoly Horvath over 9 years
    hint: replace single quotes with backticks.
  • Lightness Races in Orbit
    Lightness Races in Orbit almost 6 years
    @Spidey If you're already doing stuff like hacking pointers (some T*) into void* (and back again later), then const void* is the const equivalent (so for some const T*). Sure, you could const_cast it away but why? In other words, it's precisely as useful as any other const T* and that's "quite a lot". :)