为什么range-v3将其函数对象放入内联命名空间?

Bar*_*rry 10 c++ c++11 inline-namespaces range-v3

在range-v3中,所有函数都是内联命名空间中的全局函数对象:

#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
Run Code Online (Sandbox Code Playgroud)

function_objects命名空间的目的是什么?据我所知,它不会在库中的任何其他位置引用.

Bar*_*rry 9

基于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().

是的,这很酷.而且很复杂.

  • 有一些讨论[关于核心反射器](http://lists.isocpp.org/core/2018/04/4164.php),当我在说服自己打破[\ _ [basic.scope]后我问这是否合法时.declarative \]/4](http://eel.is/c++draft/basic.scope.declarative#4).(TLDR:内联命名空间使其合法.) (3认同)