Boost logo

Boost :

Subject: Re: [boost] namespace noncopyable_
From: Juraj Ivančić (juraj.ivancic_at_[hidden])
Date: 2008-11-19 04:28:25


Kenneth Laskoski wrote:
> Why is this nested namespace noncopyable_ necessary? The comment "//
> protection from unintended ADL" seems greek to me. Can someone please
> enlighten me and point out a practical consequence of simply defining
> "namespace boost { class noncopyable {...}; }"

I think this was the case few releases ago until someone probably got
bitten by the following scenario. Consider this example.

#include <iostream>

namespace boost
{
// Imagine that the noncopyable is in the boost namespace as you suggest
     class noncopyable { /* ... */ };

// Imagine that you also included the following function from boost.
// possibly - you have no idea as it is hidden somewhere deep in the
// include chain
     template<typename T>
     void some_func( T ) { std::cout << "Boost func."; }
}

class MyClass : public boost::noncopyable
{
};

// If
// 1. you forget to include header which declares your own
// implementation of void some_func( MyClass ) or
// 2. your some_func is in some other namespace which you
// forget to explicitly qualify
// ADL will resolve function call to the boost::some_func
// and the program will successfully compile.

int main()
{
     MyClass m;
     some_func_x( m );
}

HTH


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