【问题标题】:What is the meaning of the sentence referring to functions introduced by a using declaration?引用 using 声明引入的函数的句子是什么意思?
【发布时间】:2017-07-10 18:11:00
【问题描述】:

我正在学习 C++03 标准,现在正在阅读 [7.3.3]/11,但我无法理解以下段落:

如果命名空间范围或块范围内的函数声明具有相同的名称和相同的参数 类型作为由 using 声明引入的函数,并且声明不声明相同的函数, 程序格式不正确。

我在任何地方都没有找到任何这种情况的例子,我不明白这段话的意思。

【问题讨论】:

  • 您引用的非常标准的段落中有一个示例。你能详细说明为什么这没有回答你吗?
  • 仅供参考。 C++11 增加了一些例子。见timsong-cpp.github.io/cppwp/n3337/namespace.udecl#14
  • 嘿!如果答案对您有帮助,请不要忘记将此问题标记为已解决! :D

标签: c++ c++03


【解决方案1】:

意思是:

namespace namespace_1
{
    void foo(int number);
}
using namespace_1::foo;
void foo(int roll_no);

这意味着程序格式错误。 我相信这意味着该功能会令人困惑。有一次,函数定义会将传递的 int 用作整数(通用),但在另一种情况下,我们会将其用作 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)
}

【讨论】:

  • 我使用 C++ 的次数越多,我似乎就越少使用命名空间作为快捷方式,而且我对这样做的代码越感到恼火。
  • using namespace blablabla is not using declaration.
【解决方案2】:

以下程序包含错误

#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(){

作为对比,下面的程序是正确的。 _1 命名空间是通过 using 指令引入的。

#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 查看添加的两个示例。
  • 非常感谢。我尝试进一步区分命名空间范围、块范围、类范围中的 using 声明。我测试了类范围,所以我很困惑,但是选择范围对于这条规则很重要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 2011-09-24
相关资源
最近更新 更多