覆盖 ADL 选择的重载

override an overload selected by ADL

本文关键字:重载 选择 ADL 覆盖      更新时间:2023-10-16

我正在使用一个operator<<有缺陷的库,我想用我自己的版本替换它。它遵循 ADL 根据参数在库命名空间中的成员身份选择重载的惯用语。有什么方法可以让C++选择我自己的operator<<吗?

一个次优的解决方案是在库类型周围声明一个包装类。

一般实现如下所示:

/* Namespace-specific reference wrapper type.
   Associates a function argument with the desired namespace.
   Declare a new use_local_t for each namespace with an overriding overload */
template< typename t >
struct use_local_t
    { t ref; };
template< typename t >
use_local_t< t && >
use_local( t &&o )
    { return { std::forward< t >( o ) }; }
/* The overriding overload.
   Instead of overloading on a specialization of use_local_t, use the
   general template and enable_if. This allows for the various kinds of
   references that use_local_t might forward, and conversion of the function
   argument to the expected library_type parameter. */
template< typename t >
inline
typename std::enable_if<
    std::is_convertible< t, library_type const & >::value,
    std::ostream &
>::type
operator<< ( std::ostream &s, use_local_t< t > ul ) {
    return s << ul.ref.foo;
}
std::cout << my_namespace::use_local( library_obj );

这经过测试,可与表达式模板配合使用。请注意,如果覆盖重载不匹配,则来自 GCC 4.7 的错误消息是红鲱鱼...它是指涉及流右值引用的std::重载:

/

opt/local/include/gcc47/c++/ostream:600:5:错误:初始化参数 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&)