Saturday, August 17, 2013

Will "using declaration" introduce a conflict issue if we have a local function with an identical signature?

Newsgroup: comp.lang.c++

Subject: Will "using declaration" introduce a conflict issue if we have a local function with an identical signature?

From: junyangzou <zoujyjs@...>

Date: Fri, 16 Aug 2013 21:02:45 -0700 (PDT)



Consider the following code( an excerpt from "Effective C++, 3rd" Item 33 ):



#include <iostream>

using namespace std;



class Base{

private:

int x;

public:

virtual void mf1() = 0;

virtual void mf1(int){

cout << "mf1(int) in base" << endl;

}



virtual void mf2(){

cout << "mf2 in base" << endl;

}



void mf3(){

cout << "mf3 in base" << endl;

}



void mf3(double){

cout << "mf3(double) in base" << endl;

}

};



class Derived: public Base {

public:

using Base::mf1; // make all things in Base named mf1 and mf3

using Base::mf3; // visible (and public) in Derived's scope



virtual void mf1(){

cout << "mf1 in derived" << endl;

}



void mf3()----------------------------------(?)

{

cout << "mf3 in derived" << endl;

}



void mf4(){

cout << "mf4 in derived" << endl;

}

};







int main(){

Derived d;

int x = 0;

d.mf1(); // still fine, still calls Derived::mf1

d.mf1(x); // now okay, calls Base::mf1

d.mf2(); // still fine, still calls Base::mf2

d.mf3(); // fine, calls Derived::mf3

d.mf3(x); // now okay, calls Base::mf3

cin.get();



return 0;

}



Notice that the local mf3() has an identical signature with the synonym mf3() introduced by using Base::mf3. But this code will not get any complainant with g++.



And here in http://msdn.microsoft.com/zh-cn/library/was37tzw(v=VS.71).aspx.



Quote:

If a set of local declarations and using-declarations for a single name are given in a declarative region, they must all refer to the same entity, or they must all refer to functions. For example:

// functions_in_namespaces1.cpp

// C2874 expected

namespace B

{

int i;

void f(int);

void f(double);

}



void g()

{

int i;

using B::i; // error: i declared twice

void f(char);

using B::f; // ok: each f is a function

}

In the example above, the using B::i statement causes a second int i to be declared in the g() function. The using B::f statement does not conflict with the f(char) function because the function names introduced by B::f have different parameter types.



So, back to the title, will using introduce a conflict?







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?67695-Will-using-declaration-introduce-a-conflict-issue-if-we-have-a-local-function-with-an-identical-signature&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment