通过using声明引入的函数是什么意思?

4
我正在学习C++03标准,现在正在阅读[7.3.3]/11,但我不太理解以下段落的意思:
如果命名空间作用域或块作用域内的函数声明与使用声明引入的函数具有相同的名称和参数类型,并且这些声明未声明相同的函数,则程序将是非法的。
我没有在任何地方找到此情况的示例,也不理解这段话的含义。

2
你引用的标准段落中已经有一个例子。你能详细说明为什么那个例子不能回答你的问题吗? - Angew is no longer proud of SO
FYI。C++11添加了一些示例。请参见https://timsong-cpp.github.io/cppwp/n3337/namespace.udecl#14。 - R Sahu
嘿!如果答案对你有帮助,请不要忘记将此问题标记为已解决! :D - Alpha Mineron
2个回答

5
这意味着:
namespace namespace_1
{
    void foo(int number);
}
using namespace_1::foo;
void foo(int roll_no);

这意味着程序格式不正确。我认为这是指函数会让人难以理解。因为在某个点上,该函数定义将使用传递的整数作为整数(通用),但在另一种情况下,我们将使用它作为roll_no。
这也会导致重载函数匹配中的歧义。
您引用的源代码在您引用的行下面给出了一个示例:
namespace B {
  void f(int);
  void f(double);
}
namespace C {
  void f(int);
  void f(double);
  void f(char);
}
void h() {
  using B::f;       // B::f(int) and B::f(double)
  using C::f;       // C::f(int), C::f(double), and C::f(char)
  f('h');           // calls C::f(char)
  f(1);             // error: ambiguous: B::f(int) or C::f(int)?
  void f(int);      // error: f(int) conflicts with C::f(int) and B::f(int)
}

1
一些额外阅读:http://en.cppreference.com/w/cpp/language/namespace#Using-declarations - Charles
2
我使用C++的越多,我似乎就越少使用命名空间来进行快捷操作,而且我对使用命名空间的代码感到越来越烦恼。 - kayleeFrye_onDeck
使用命名空间blablabla不是使用声明。 - Pupkin

3
以下程序存在错误。
#include <iostream>

namespace _1{
    int f(){
        std::cout << "_1::f\n";
    }
}

namespace _2{
/*
*If a function declaration in namespace scope or block scope has the 
*same name and the same parameter types as a function introduced by
* a using-declaration
*/
    using _1::f;
// This is not the same function as introduced by the using directive
    int f(){
        std::cout << "_2::f\n";
    }
}

int main(){
    _2::f();
}

诊断结果是:
main.cpp: In function ‘int _2::f()’:
main.cpp:13:11: error: ‘int _2::f()’ conflicts with a previous declaration
     int f(){

作为对比,以下程序是正确的。通过using指令引入了_1命名空间。
#include <iostream>

namespace _1{
    int f(){
        std::cout << "_1::f\n";
    }
}

namespace _2{
    using namespace _1;

    int f(){
        std::cout << "_2::f\n";
    }
}

int main(){
    _2::f();
}

预期输出结果为

_2::f

在块级作用域中,您有相同的情况。
#include <iostream>

namespace _1{
    int f(){
        std::cout << "_1::f\n";
    }
}

namespace _2{

    int g(){
// As before but in block scope.
        using _1::f;

        int f();
        f();
    }
    int f(){
        std::cout << "_2::f\n";        
    }

}

int main(){
    _2::f();
}

诊断程序与先前的版本完全相同。
main.cpp: In function ‘int _2::g()’:
main.cpp:15:15: error: ‘int _2::f()’ conflicts with a previous declaration
         int f();
               ^

上面成功示例的并行构造将是:
#include <iostream>

namespace _1{
    int f(){
        std::cout << "_1::f\n";
    }
}

namespace _2{

    int g(){
        using namespace _1;

        int f();
        f();
    }
    int f(){
        std::cout << "_2::f\n";        
    }

}

int main(){
    _2::g();
}

有了输出

_2::f

谢谢您清楚的解释。但是关于块作用域呢?我在块作用域上得不到相同的结果。 - Pupkin
@Pupkin 请查看添加的两个样例。 - Captain Giraffe
非常感谢。我尝试进一步区分在命名空间作用域、块作用域和类作用域中使用声明的差异。我已经测试了类作用域,所以我感到困惑,但是选择正确的作用域对于这个规则很重要。 - Pupkin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接