What is the purpose of this namespace?

431 Views Asked by At

In Boost 1.55, current_function.hpp reads something like this:

namespace boost
{
namespace detail
{
inline void current_function_helper()
{

#if defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) || (defined(__ICC) && (__ICC >= 600)) || defined(__ghs__)

# define BOOST_CURRENT_FUNCTION __PRETTY_FUNCTION__

#elif ...

#endif

}    
} // namespace detail
} // namespace boost

Why did the author even bother writing those namespaces?

1

There are 1 best solutions below

2
On BEST ANSWER

detail namespaces help to avoid polluting the official ones with internal functions or classes the user doesn't need to know of.

Modern IDEs parse Header files and provide code completion, that is, suggestions for names if you start to write something like boost::c. That would be quite unusable if internal algorithms such as e.g. copy_pod_nontrivial would also be listed.

Moreover, a typo could lead to a call to an internal function or a declaration of an object of an internal class type. That is definitely not what we want.

A more technical reason for this is ADL: Some functions are found by the type of their arguments (template arguments for function templates are also involved); That can cause name lookup to search for names in the official namespace. If helper functions (or classes) are declared within it, this may cause problems with overload resolution.

Users should never (need to) use those internal functions, so if you see user code that accesses a detail-like namespace you should be on guard.