加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 3.4.2 n3290草稿中的参数相关名称查找

发布时间:2020-12-16 05:45:25 所属栏目:百科 来源:网络整理
导读:来自ISO草案n3290第3.4.2段第1段: When the postfix-expression in a function call is an unqualified-id ,other namespaces not considered during the usual unqualified lookup may be searched,and in those namespaces,namespace-scope friend functi
来自ISO草案n3290第3.4.2段第1段:

When the postfix-expression in a function call is an unqualified-id,other namespaces not considered during the usual unqualified lookup may be searched,and in those namespaces,namespace-scope friend function declarations not otherwise visible may be found. These modifications to the search depend on the types of the arguments (and for template template arguments,the namespace of the template argument).

在这里他们说,“这些对搜索的修改取决于参数/模板模板参数/模板参数的命名空间的类型”…可以有任何一个具有示例的expalin吗?我尝试用argumetn类型.使用模板模板参数类型&模板参数类型的命名空间

解决方法

考虑一个简单的不合格的函数调用:
foo(x);

ADL意味着foo不仅在封闭的范围内查找,而且调用所在的命名空间也被查找,而且也是x类型的命名空间.例如如果x是一个std :: vector< int>那么还会搜索namespace std.从而:

int main() {
    std::vector<int> x,y;
    swap(x,y);
}

可以,并调用std :: swap().

查找也取决于任何模板参数的命名空间,因此如果x是std :: vector< mynamespace :: myclass>那么mynamespace也包含在查找中.从而

namespace mynamespace {
    struct myclass {};
    void foo(std::vector<mynamespace::myclass> const&){}
}

int main() {
    std::vector<mynamespace::myclass> x;
    foo(x);
}

会调用mynamespace :: foo().

最后,查找也扩展到用作模板模板参数的任何模板的命名空间.例如

namespace mynamespace {
    template<typename T>
    struct mytemplate
    {};

    template<typename T>
    void bar(T const&) {}
}

template<template<typename> class T>
struct wrapper {};

int main() {
    wrapper<mynamespace::mytemplate> x;
    bar(x);
}

即使包装器在全局命名空间中,将会找到mynamespace :: bar,因为用于x的模板模板参数是mynamespace :: mytemplate.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读