Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 引用using声明引入的函数的句子是什么意思?_C++_C++03 - Fatal编程技术网

C++ 引用using声明引入的函数的句子是什么意思?

C++ 引用using声明引入的函数的句子是什么意思?,c++,c++03,C++,C++03,我正在学习C++03标准,现在正在阅读[7.3.3]/11,但我在理解以下段落时遇到困难: 如果命名空间范围或块范围中的函数声明具有相同的名称和参数 类型作为using声明引入的函数,且声明不声明相同的函数, 这个程序格式不好 我在任何地方都找不到这种情况的例子,我也不明白这段话的意思。它的意思是: namespace namespace_1 { void foo(int number); } using namespace_1::foo; void foo(int roll_no);

我正在学习C++03标准,现在正在阅读
[7.3.3]/11
,但我在理解以下段落时遇到困难:

如果命名空间范围或块范围中的函数声明具有相同的名称和参数 类型作为using声明引入的函数,且声明不声明相同的函数, 这个程序格式不好

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

它的意思是:

namespace namespace_1
{
    void foo(int number);
}
using namespace_1::foo;
void foo(int roll_no);
这意味着程序的格式不正确。 我相信这意味着函数读起来会很混乱。在某一点上,函数定义将使用传递的int作为整数(一般),但在另一种情况下,我们将使用它作为roll\u 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)
}

以下程序包含该错误

#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();
}
作为对比,以下程序是正确的。_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();
}
对于块范围内的相同情况,您有

#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();
}
上述成功样本的并行构造为

#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();
}

我使用C++越多,我就越不使用快捷方式的命名空间,而且我越讨厌代码。使用命名空间BLabLaba不使用声明。你能详细解释一下为什么这不能回答你吗?仅供参考。C++11添加了一些示例。看,嘿!如果答案对您有帮助,请不要忘记标记此问题已解决!:谢谢你的澄清。但是块范围呢。我不能在块范围上得到相同的结果。@Pupkin看到添加的两个示例。非常感谢。我试图进一步区分命名空间范围、块范围和类范围中的using声明。我测试了类的作用域,所以我很困惑,但是选择一个作用域对于这个规则很重要。
#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