Will "using declaration" introduce a conflict issue if we have alocal function with an identical sig


J

junyangzou

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?
 
Ad

Advertisements

V

Victor Bazarov

[..]

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

No. The Standard allows it, see [namespace.udecl]/15: <<When a
using-declaration brings names from a base class into a derived class
scope, member functions and
member function templates in the derived class override and/or hide
member functions and member function
templates with the same name, parameter-type-list (8.3.5),
cv-qualification, and ref-qualifier (if any) in a
base class (rather than conflicting).>>

V
 
J

junyangzou

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



No. The Standard allows it, see [namespace.udecl]/15: <<When a

using-declaration brings names from a base class into a derived class

scope, member functions and

member function templates in the derived class override and/or hide

member functions and member function

templates with the same name, parameter-type-list (8.3.5),

cv-qualification, and ref-qualifier (if any) in a

base class (rather than conflicting).>>



V

Thanks for posting a standard reference. And for data member like the example in MSDN, this is not true? Actually, I've tried to compile the code above in VS 2012, the compiler complains a name conflict.
 
Ad

Advertisements

V

Victor Bazarov

[..]
Thanks for posting a standard reference. And for data member like the
example in MSDN, this is not true? Actually, I've tried to compile
the code above in VS 2012, the compiler complains a name conflict.

The Standard does not make any specific claim about data members. I
suppose it might be by mistake, but might be that nobody thought of
mentioning it because that's not the usual use for a 'using'
declaration. I'd say, since nothing is said either way, any behavior of
the compiler is OK in that case. Or maybe I couldn't find it...

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top