Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the sentence referring to functions introduced by a using declaration?

Tags:

c++

c++03

I'm studying the C++03 Standard and am now reading [7.3.3]/11, but I'm having trouble understanding the following passage:

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, and the declarations do not declare the same function, the program is ill-formed.

I haven't found any examples of this situation anywhere and I don't understand the meaning of this passage.

like image 667
Pupkin Avatar asked Jan 31 '23 00:01

Pupkin


2 Answers

What it means is that:

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

This means the program is ill-formed. I believe it means to say that the function would be confusing to read. As at one point, the function definition would be using the passed int as an integer(general) but in the other case, we'd be using it as a roll_no.

This would also cause ambiguity in overloaded function matching.

The source you are citing gives an example just below the lines you have cited:

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)
}
like image 131
Alpha Mineron Avatar answered Feb 08 '23 15:02

Alpha Mineron


The following program contains the error

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

The diagnostic is

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

As a contrast, the following program is correct. The _1 namespace is introduced via a using directive.

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

With the expected output

_2::f

As for the same situation in block scope you have

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

The diagnostic is identical

main.cpp: In function ‘int _2::g()’:
main.cpp:15:15: error: ‘int _2::f()’ conflicts with a previous declaration
         int f();
               ^

The parallel construct of the successful sample above would be

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

With the output

_2::f
like image 22
Captain Giraffe Avatar answered Feb 08 '23 15:02

Captain Giraffe