Topic: using-declaration: position relevant?


Author: stip@mathematik.uni-ulm.de (Alexander Stippler)
Date: Wed, 25 Jun 2003 17:55:12 +0000 (UTC)
Raw View
Hello,

my question is, if the position of a using-declaration to "redeclare" an
inherited member method does matter (before or after the method which would
otherwise hide the inherited member). I read the relevant section in the
standard (7.3.3), but I do not know more than before. Example:

class A
{
        public:
                void
                foo() const;
};

class B
        : public A
{
        public:
                // using here ...

                void
                foo();

                using A::foo; // ... or here? Does it matter at all?
};

In my understanding of the standard, it does matter. I think, the using must
be used after the particular method, since the inherited method would be
hided again, otherwise. What does the standard really say in this respect?

regards,
        Alex


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Thu, 26 Jun 2003 02:19:50 +0000 (UTC)
Raw View
"Alexander Stippler" <stip@mathematik.uni-ulm.de> wrote...
> my question is, if the position of a using-declaration to "redeclare" an
> inherited member method does matter (before or after the method which
would
> otherwise hide the inherited member). I read the relevant section in the
> standard (7.3.3), but I do not know more than before. Example:
>
> class A
> {
>         public:
>                 void
>                 foo() const;
> };
>
> class B
>         : public A
> {
>         public:
>                 // using here ...
>
>                 void
>                 foo();
>
>                 using A::foo; // ... or here? Does it matter at all?
> };
>
> In my understanding of the standard, it does matter. I think, the using
must
> be used after the particular method, since the inherited method would be
> hided again, otherwise. What does the standard really say in this respect?

"using" acts like a declaration.  In some situations the position
of it doesn't matter, in some it does.

Example where it doesn't matter:

    struct A {
        void foo();
    };

    struct B : A {
        void foo(int);
        using A::foo; // brings 'foo' to this class' scope
        void bar();
    };

    void B::bar() {
        foo(42);
        foo();    // would be an error without "using"
    }

Example where it matters:

    namespace A {
        void foo();
    }

    namespace B {
        void foo(int);
    }

    void bar() {
        using A::foo;
        foo();
        foo(42);  // error -- no foo(int) in scope
        using B::foo;
        foo(73);  // OK -- foo is overloaded
    }

I can't think of an example where the position of a 'using'
declaration would matter in a class definition.

Victor


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rmaddox@isicns.com (Randy Maddox)
Date: Thu, 26 Jun 2003 14:42:49 +0000 (UTC)
Raw View
stip@mathematik.uni-ulm.de (Alexander Stippler) wrote in message news:<3ef95c3c@news.uni-ulm.de>...
> Hello,
>
> my question is, if the position of a using-declaration to "redeclare" an
> inherited member method does matter (before or after the method which would
> otherwise hide the inherited member). I read the relevant section in the
> standard (7.3.3), but I do not know more than before. Example:
>
> class A
> {
>         public:
>                 void
>                 foo() const;
> };
>
> class B
>         : public A
> {
>         public:
>                 // using here ...
>
>                 void
>                 foo();
>
>                 using A::foo; // ... or here? Does it matter at all?
> };
>
> In my understanding of the standard, it does matter. I think, the using must
> be used after the particular method, since the inherited method would be
> hided again, otherwise. What does the standard really say in this respect?
>
> regards,
>         Alex
>
>

I believe it shouldn't matter where the using declaration is put, but
in this particular case B::foo() is going to hide A::foo() with or
without any using declaration.  See the example in 7.3.3/12.  That is,
a using declaration really has no effect when the function
declarations match exactly (other than the this pointer).  If you had
instead B::foo(int), then using A::foo() would allow you to overload
foo() in class B so that you could call either foo() or foo(int).

Randy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: fresl@grad.hr (Kresimir Fresl)
Date: Thu, 26 Jun 2003 16:22:30 +0000 (UTC)
Raw View
Randy Maddox wrote:

> Alexander Stippler wrote:

>>my question is, if the position of a using-declaration to "redeclare" an
>>inherited member method does matter (before or after the method which would
>>otherwise hide the inherited member). I read the relevant section in the
>>standard (7.3.3), but I do not know more than before. Example:
>>
>>class A
>>{
>>        public:
>>                void
>>                foo() const;
>>};
>>
>>class B
>>        : public A
>>{
>>        public:
>>                // using here ...
>>
>>                void
>>                foo();
>>
>>                using A::foo; // ... or here? Does it matter at all?
>>};

> I believe it shouldn't matter where the using declaration is put, but
> in this particular case B::foo() is going to hide A::foo() with or
> without any using declaration.  See the example in 7.3.3/12.  That is,
> a using declaration really has no effect when the function
> declarations match exactly (other than the this pointer).

A::foo() is const
B::foo() is non-const

fres



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@attAbi.com ("Victor Bazarov")
Date: Fri, 27 Jun 2003 17:57:37 +0000 (UTC)
Raw View
"Randy Maddox" <rmaddox@isicns.com> wrote...
> stip@mathematik.uni-ulm.de (Alexander Stippler) wrote in message
news:<3ef95c3c@news.uni-ulm.de>...
> > Hello,
> >
> > my question is, if the position of a using-declaration to "redeclare" an
> > inherited member method does matter (before or after the method which
would
> > otherwise hide the inherited member). I read the relevant section in the
> > standard (7.3.3), but I do not know more than before. Example:
> >
> > class A
> > {
> >         public:
> >                 void
> >                 foo() const;
> > };
> >
> > class B
> >         : public A
> > {
> >         public:
> >                 // using here ...
> >
> >                 void
> >                 foo();
> >
> >                 using A::foo; // ... or here? Does it matter at all?
> > };
> >
> > In my understanding of the standard, it does matter. I think, the using
must
> > be used after the particular method, since the inherited method would be
> > hided again, otherwise. What does the standard really say in this
respect?
> >
> > regards,
> >         Alex
> >
> >
>
> I believe it shouldn't matter where the using declaration is put, but
> in this particular case B::foo() is going to hide A::foo() with or
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> without any using declaration.  See the example in 7.3.3/12.  That is,
> a using declaration really has no effect when the function
> declarations match exactly (other than the this pointer).  If you had
> instead B::foo(int), then using A::foo() would allow you to overload
> foo() in class B so that you could call either foo() or foo(int).

I am sorry, I must miss something in 7.3.3.  In this particular case,
A::foo is a const function and B::foo isn't.  So, with the "using"
declaration in place there is no hiding, the functions' types are, in
fact, different.

Could you please show what I am wrong about if I am wrong somewhere in
my explanation?  Thanks.

-------------------------------- Example code
struct A {
   A() {}
   void foo() const {}
};

struct B : A {
   B() {}
   void foo() {}
   using A::foo;
};

int main() {
   const B b;
   b.foo();   // will cause error without "using"
}
------------------------------------------------

Victor


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: rmaddox@isicns.com (Randy Maddox)
Date: Sat, 28 Jun 2003 00:52:16 +0000 (UTC)
Raw View
fresl@grad.hr (Kresimir Fresl) wrote in message news:<3EFB0A6E.7030207@grad.hr>...
> Randy Maddox wrote:
>
> > Alexander Stippler wrote:
>
> >>my question is, if the position of a using-declaration to "redeclare" an
> >>inherited member method does matter (before or after the method which would
> >>otherwise hide the inherited member). I read the relevant section in the
> >>standard (7.3.3), but I do not know more than before. Example:
> >>
> >>class A
> >>{
> >>        public:
> >>                void
> >>                foo() const;
> >>};
> >>
> >>class B
> >>        : public A
> >>{
> >>        public:
> >>                // using here ...
> >>
> >>                void
> >>                foo();
> >>
> >>                using A::foo; // ... or here? Does it matter at all?
> >>};
>
> > I believe it shouldn't matter where the using declaration is put, but
> > in this particular case B::foo() is going to hide A::foo() with or
> > without any using declaration.  See the example in 7.3.3/12.  That is,
> > a using declaration really has no effect when the function
> > declarations match exactly (other than the this pointer).
>
> A::foo() is const
> B::foo() is non-const
>
> fres
>
>

Ooops!  You are absolutely correct.  I missed that detail.  :-)

Randy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]