本文介绍了ADL是否可用于全局名称空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例 启用std类型的输出说明 ADL 用于注入"特定的函数/运算符,具体取决于fn/op所适用的类型.

我想知道ADL是否完全适用于全局名称空间,即是否在使ADL在全局名称空间中寻找匹配的功能吗?

具体来说,这些等效的是wrt. ADL?:

// 1 - at global namespace scope
struct GlobalType {};

template< class Ch, class Tr>
std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, GlobalType const& x)
{
    os << ...;
    return os;
}

// 2 - within namespace
namespace ecaps {

    struct EcapsType {};

    template< class Ch, class Tr>
    std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, EcapsType const& x)
    {
        os << ...;
        return os;
    }

}

// 3 - Type brought to global NS via using, function at global scope
namespace other {
    struct OtherType {};
}

using other::OtherType;

template< class Ch, class Tr>
std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, OtherType const& x)
{
    os << ...;
    return os;
}


Wrt.全局名称空间范围不需要ADL :(在已删除答案后更新)

一个委员会名望的丹尼尔·克鲁格勒(DanielKrügler)描述这样的ADL问题:

Emph.矿.请注意,如何将外部名称空间描述为仅从词法位置"... ..."开始.他继续:

大胆的雌雄同体.矿.因此在我看来, 与ADL适用于全局名称空间有关.当然,我很容易误会了一些东西.


注意:这可能是标准的一种情况,只是没有明确地一种或另一种方式提及它,因为全局NS就像任何其他名称空间一样-再次,我所知该标准的内容非常有限.

解决方案

完全忘记了我最初的答案,这是完全错误的.

根据C ++ 11标准,关于ADL(重点是我)的§3.4.2:

简而言之,由于不合格查找将始终在全局名称空间中搜索,因此 ADL将从不应用于全局名称空间.

Examples such as enabling outputting of std types explain how ADL can be used to "inject" a certain function/operator, depending on the type the fn/op is applied to.

I was wondering wheter ADL fully applies to the global namespace, that is, whether a type declared (or made available via using) at global namespace scope makes ADL look for matching functions in the global namespace?

Specifically, are these equivalent wrt. ADL?:

// 1 - at global namespace scope
struct GlobalType {};

template< class Ch, class Tr>
std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, GlobalType const& x)
{
    os << ...;
    return os;
}

// 2 - within namespace
namespace ecaps {

    struct EcapsType {};

    template< class Ch, class Tr>
    std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, EcapsType const& x)
    {
        os << ...;
        return os;
    }

}

// 3 - Type brought to global NS via using, function at global scope
namespace other {
    struct OtherType {};
}

using other::OtherType;

template< class Ch, class Tr>
std::basic_ostream<Ch, Tr>& operator<<(std::basic_ostream<Ch, Tr>& os, OtherType const& x)
{
    os << ...;
    return os;
}


Wrt. global namespace scope not needing ADL: (update after a now deleted answer)

One Daniel Krügler of Committee fame describes an ADL problem as such:

Emph. mine. Note how the outer namespaces are described to only be considered "... from the lexical location ...". He continues:

Bold emph. mine. So it would seem to me it is relevant that ADL works for the global namespace. Of course I could easily have misunderstood something.


Note: This may be a case of the standard simply not explicitly mentioning it one way or another, because the global NS is just like any other namespace -- then again it may not, my knowledge of the Standard is very limited.

解决方案

Completely forget my initial answer, it was plain wrong.

From the C++11 standard, §3.4.2 on ADL (emphasis mine):

So in short, since unqualified lookup will always search in the global namespace, ADL will never apply to global namespace.

这篇关于ADL是否可用于全局名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:23
查看更多