Bar*_*rry 10 c++ c++11 inline-namespaces range-v3
在range-v3中,所有函数都是内联命名空间中的全局函数对象:
Run Code Online (Sandbox Code Playgroud)#if RANGES_CXX_INLINE_VARIABLES < RANGES_CXX_INLINE_VARIABLES_17 #define RANGES_INLINE_VARIABLE(type, name) \ inline namespace function_objects \ { \ inline namespace \ { \ constexpr auto &name = ::ranges::static_const<type>::value; \ } \ } #else // RANGES_CXX_INLINE_VARIABLES >= RANGES_CXX_INLINE_VARIABLES_17 #define RANGES_INLINE_VARIABLE(type, name) \ inline namespace function_objects \ { \ inline constexpr type name{}; \ } #endif // RANGES_CXX_INLINE_VARIABLES
function_objects命名空间的目的是什么?据我所知,它不会在库中的任何其他位置引用.
基于Casey对PR的评论导致了这一增加(感谢Justin),这inline namespace对于这样的事情是必要的:
namespace N {
namespace detail {
// default implementation
template <typename T> void swap(T&, T& ) { ... }
struct swap_fn {
template <typename T>
void operator()(T& a, T& b) {
swap(a, b); // unqualified call, the default is in scope
}
};
}
// the object
inline namespace CPO { inline constexpr detail::swap_fn swap{}; } // #1
struct S { friend void swap(S&, S&) { ... } }; // #2
}
N::S a;
N::swap(a, a); // calls the CPO, which calls the friend function
Run Code Online (Sandbox Code Playgroud)
如果自定义点对象swap(at #1)不在其自己的命名空间中,则cpo和friend为type S(at #2)声明的非成员之间将存在名称冲突.这些必须位于不同的名称空间中.
将CPO放在内联命名空间中就足够了,因为常规的非限定或限定查找永远不会找到交换#2- 它只能由ADL找到,它只发生在内swap_fn::operator().
是的,这很酷.而且很复杂.