在range-v3中,所有函数实际上都是内联命名空间中的全局函数对象。
#如果RANGES_CXX_INLINE_VARIABLES < RANGES_CXX_INLINE_VARIABLES_17 #定义RANGES_INLINE_VARIABLE(类型,名称)\内联名称空间function_objects \{内联名称空间\{auto &name = ::ranges::static_const::value;\} #define // RANGES_CXX_INLINE_VARIABLES >= RANGES_CXX_INLINE_VARIABLES_17 #定义RANGES_INLINE_VARIABLE(类型,名称)\内联命名空间function_objects \\ inline类型名称{};#endif // RANGES_CXX_INLINE_VARIABLES
function_objects
命名空间的用途是什么?据我所知,在图书馆的其他任何地方都没有引用它。
发布于 2018-04-24 21:51:50
根据凯西对按下的评论(谢谢贾斯汀),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
如果定制点对象swap
(at #1
)不在其自己的命名空间中,则cpo与为S
(at #2
)类型声明的非会员friend
之间将出现名称冲突。它们必须位于不同的命名空间中。
将CPO放入内联命名空间就足够了,因为常规的不合格或限定的查找永远不会在#2
找到这个交换空间--它只会由ADL找到,而这种情况只发生在swap_fn::operator()
中。
是啊,这挺酷的。也很复杂。
https://stackoverflow.com/questions/50010074
复制相似问题