gpt4 book ai didi

c++ - ADL 和类型定义

转载 作者:行者123 更新时间:2023-11-28 04:02:42 25 4
gpt4 key购买 nike

简而言之,我试图了解 C++ 中参数依赖查找的行为。我不清楚 ISO/IEC 14882:2017 (E) 中关于 ADL 的一些陈述。我希望有人能向我澄清它们。

按照标准,

Typedef names and using-declarations used to specify the types do not contribute to this set.

When considering an associated namespace, the lookup is the same as the lookup performed when the associated namespace is used as a qualifier (6.4.3.2) except that:

  • Any using-directive s in the associated namespace are ignored...

对我来说,这些陈述意味着 ADL 应该完全忽略任何 typedefusing 的出现。可能情况并非如此。考虑以下示例:

#include <iostream>                                                             

using namespace std;

namespace N2
{
struct B {};

template <typename T>
void func (const T&) {cout << __PRETTY_FUNCTION__ << endl;}
};

namespace N
{
typedef N2::B C;
}

void tfunc (N::C) {}

int main ()
{
func(tfunc);
}

它有效,即编译器能够找到 func。那么,标准中的这些引述究竟意味着什么?

最佳答案

此答案由@IgorTandetnik 提供。

标准所说的是 N2tfunc 的关联命名空间之一,但 N 不是。换句话说,void tfunc (N::C)void tfunc(N2::B) 完全相同。如果在您的示例中,您将 func 移动到 N,则不会找到它,尽管从表面上看,tfunc 的声明提到了 N.

关于c++ - ADL 和类型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59246027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com