Normally, const
modifies what immediately precedes it (and
should always be written immediately after what it modifies).
In the cases you show:
const void f();
The const is ignored. Don't write things like this; it confuses
the reader.
void f() const;
This declares a "const" function (because the const
is
immediately preceded by a function declaration. The notion of
a const function is a bit particular: it only applies to
non-static member functions, and it means that the type of
this
will be T const*
, rather than T*
. In practice, it is
taken as a promise not to modify the observable state of the
object the function is called on.
const void f() const;
Exactly the same as the precedent. The first const
is
ignored.
There are, of course, many other places const
can appear:
void const* f();
for example, declares a function which returns a pointer to
a const void. (You will often see this written as
const void* f();
If nothing precedes the const
, then it applies to what
follows. As a general rule, however, it is preferable to avoid
this style, even if it is quite widespread.)
Note the difference with respect to what you wrote as the first
example. Here, the return type is a pointer, and the const
applies to what is pointed to, not to the pointer (which would
be void *const
). While top level const
is ignored on
non-class return types (so void *const f();
is the same as
void* f();
), this is not true for other const.