What is the meaning of the sentence related to the functions introduced by the declaration?

I am studying the C ++ 03 standard and [7.3.3]/11am reading it now , but it’s hard for me to understand the following passage:

If a function declaration in the namespace or block region has the same name and the same parameter types as functions entered using the declaration-declaration, and the declarations do not declare the same function, the program is poorly formed.

I have not found examples of this situation anywhere, and I do not understand the meaning of this passage.

+4
source share
2 answers

What does it mean:

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)
}
+5

#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
+3

Source: https://habr.com/ru/post/1681140/


All Articles