微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

来自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.

原文地址:https://www.jb51.cc/c/113799.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐