Boost logo

Boost :

Subject: Re: [boost] https://svn.boost.org/trac/boost/ticket/4515 Nasty naming conundrum? (Or does C++ need a 'not using ...; ' statement?)
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2010-08-31 05:36:50


On 31/08/2010 09:55, Paul A. Bristow wrote:
> https://svn.boost.org/trac/boost/ticket/4515 has uncovered a nasty example
> of name ambiguity caused by the addition of<random> to the Standard
> Library. As the Standard Library expands, it may not be the last:-(
>
> http://tinyurl.com/2blmq3l gives an example of details of the ambiguity of
> the name 'binomial_distribution'.
>
> "error: reference to ???binomial_distribution??? is ambiguous"
>
> Random library defines 'binomial_distribution' and so 'using namespace
> std;' brings this name into std scope implicitly because it is a library
> item name (explicitly if random is included or perhaps implicitly by some
> '#include<std>;' declaration).

Well yeah, if you pull two namespaces that contain the same names into
the global namespace, you get collisions.

The solution is easy:
- don't do 'using namespace'
- if you really insist, prefer 'using ns::name'
- if you really want to pull a namespace or a name from a namespace,
prefer doing so at local scope

Doing 'using namespace std' is even worse, because the std namespace is
quite huge and there is no portable way to know what headers include
what names, so it's really begging for collisions to happen.

>
> But Boost.Math also defines the name 'binomial_distribution' but in the
> namespace boost::math.
>
> So this implies that it may be necessary to fully qualify
> boost::math::binomial_distribution at all uses. Ugh!

It's not the end of the world, and actually quite recommended.
If boost::math is too long for you, you can alias it to bm for example.

In particular, ADL is extremely dangerous, so unless you want the lookup
to depend on ADL, you should qualify.

> Solution are:
>
> 1 Always fully qualify names like "boost:math::binomial_distribution<>
> my_bd;' This is hideously verbose.
>
> 2 Use " std::cout;" etc (instead of "using namespace std;" and "cout<<..."
> , but it gets tediously verbose.

Come on, it's not that verbose, and you need to do that in headers anyway.
'std::' is pretty short too, and it allows to quickly tell where a name
comes from.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk