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

Discussion in 'C++' started by junyangzou, Aug 17, 2013.

  1. junyangzou

    junyangzou Guest

    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?
    junyangzou, Aug 17, 2013
    #1
    1. Advertising

  2. Re: Will "using declaration" introduce a conflict issue if we havea local function with an identical signature?

    On 8/17/2013 12:02 AM, junyangzou wrote:
    > [..]
    >
    > 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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 19, 2013
    #2
    1. Advertising

  3. junyangzou

    junyangzou Guest

    On Monday, August 19, 2013 9:58:07 PM UTC+8, Victor Bazarov wrote:
    > On 8/17/2013 12:02 AM, junyangzou wrote:
    >
    > > [..]

    >
    > >

    >
    > > 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
    >
    > --
    >
    > I do not respond to top-posted replies, please don't ask


    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.
    junyangzou, Aug 21, 2013
    #3
  4. Re: Will "using declaration" introduce a conflict issue if we havea local function with an identical signature?

    On 8/20/2013 8:56 PM, junyangzou wrote:
    >[..]
    > 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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 21, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luminari
    Replies:
    23
    Views:
    786
    Wings
    Feb 11, 2007
  2. mzdude
    Replies:
    1
    Views:
    268
    mzdude
    Oct 8, 2008
  3. charles cashion

    css conflict (or html conflict)

    charles cashion, Feb 18, 2009, in forum: HTML
    Replies:
    2
    Views:
    776
    charles cashion
    Feb 18, 2009
  4. matchstick86

    sig : process vs. process(sig)

    matchstick86, Oct 12, 2009, in forum: VHDL
    Replies:
    1
    Views:
    551
    power_hf2005
    Oct 13, 2009
  5. mk
    Replies:
    4
    Views:
    543
    Steven D'Aprano
    Feb 25, 2010
Loading...

Share This Page