In cppreference, the following text is found:
Each member of an inline namespace can be partially specialized , explicitly instantiated or explicitly specialized as if it were a member of the enclosing namespace.
Note: the rule about specializations allows library versioning: different implementations of a library template may be defined in different inline namespaces, while still allowing the user to extend the parent namespace with an explicit specialization of the primary template.
What do these statements imply? Can someone explain by means of a simple example?
Consider a stupid example:
If you run this, you'll get the following output:
This is because calling
foo::bar
with anint
is specialized infoo
, even though the default implementation exists infoo::v1
.This example is useless, but consider a scenario where you wanted to specialize a
template
function orclass
in an external library (including the stl). You don't know ifvector
is a member ofstd
orstd::cxx11
(libc++ usesstd::__1
for many things). Since aninline namespace
is a way to provide versioning at the API level (e.g., you change yourinline namespace
tov2
and leavev1
alone), this lets end-users specialize without knowing the details of theinline
dnamespace
s.