Inheriting from 'T const' is -- OK or not?

Discussion in 'C++' started by Alf P. Steinbach, May 4, 2010.

  1. I have code essentially equivalent to the inheritance below:


    <code>
    #include <iostream>
    using namespace std;

    class Base
    {
    public:
    void foo() { cout << "Non-const foo" << endl; }
    void foo() const { cout << "Const foo" << endl; }
    };

    template< class T >
    class Derived: public T
    {
    public:
    using T::foo;

    void m() { foo(); }
    };

    int main()
    {
    Derived< Base const>().m();
    }
    </code>


    Seems to work nicely, as if just ignoring the const qualification, and Comeau
    doesn't complain (Comeau is usually right about such things).

    But I can't find anything in the standard supporting it, and indeed when T is
    not a template parameter but a typedef for 'Base const' then g++ rejects it.

    So, is the above standard-conforming code or does it (at least formally) need to
    "deconstify" the template parameter?


    Cheers,

    - Alf
     
    Alf P. Steinbach, May 4, 2010
    #1
    1. Advertising

  2. Alf P. Steinbach

    Öö Tiib Guest

    On 4 mai, 23:52, "Alf P. Steinbach" <> wrote:
    > I have code essentially equivalent to the inheritance below:
    >
    > <code>
    > #include <iostream>
    > using namespace std;
    >
    > class Base
    > {
    > public:
    >      void foo() { cout << "Non-const foo" << endl; }
    >      void foo() const { cout << "Const foo" << endl; }
    >
    > };
    >
    > template< class T >
    > class Derived: public T
    > {
    > public:
    >      using T::foo;
    >
    >      void m() { foo(); }
    >
    > };
    >
    > int main()
    > {
    >      Derived< Base const>().m();}
    >
    > </code>
    >
    > Seems to work nicely, as if just ignoring the const qualification, and Comeau
    > doesn't complain (Comeau is usually right about such things).
    >
    > But I can't find anything in the standard supporting it, and indeed when T is
    > not a template parameter but a typedef for 'Base const' then g++ rejects it.
    >
    > So, is the above standard-conforming code or does it (at least formally) need to
    > "deconstify" the template parameter?


    You can not derive a class from "Base const" type. So that feels
    strange if you supply "Base const" as template argument that is
    expected to be used for base class of something by template. I vaguely
    remember standard saying something about ignoring cv-qualification at
    such places, but can not find it from real thing. From standpoint of
    style i would deconstify it anyway even if it was legal.
     
    Öö Tiib, May 4, 2010
    #2
    1. Advertising

  3. On 05.05.2010 00:02, * Öö Tiib:
    > On 4 mai, 23:52, "Alf P. Steinbach"<> wrote:
    >> I have code essentially equivalent to the inheritance below:
    >>
    >> <code>
    >> #include<iostream>
    >> using namespace std;
    >>
    >> class Base
    >> {
    >> public:
    >> void foo() { cout<< "Non-const foo"<< endl; }
    >> void foo() const { cout<< "Const foo"<< endl; }
    >>
    >> };
    >>
    >> template< class T>
    >> class Derived: public T
    >> {
    >> public:
    >> using T::foo;
    >>
    >> void m() { foo(); }
    >>
    >> };
    >>
    >> int main()
    >> {
    >> Derived< Base const>().m();}
    >>
    >> </code>
    >>
    >> Seems to work nicely, as if just ignoring the const qualification, and Comeau
    >> doesn't complain (Comeau is usually right about such things).
    >>
    >> But I can't find anything in the standard supporting it, and indeed when T is
    >> not a template parameter but a typedef for 'Base const' then g++ rejects it.
    >>
    >> So, is the above standard-conforming code or does it (at least formally) need to
    >> "deconstify" the template parameter?

    >
    > You can not derive a class from "Base const" type. So that feels
    > strange if you supply "Base const" as template argument that is
    > expected to be used for base class of something by template.


    The use case is a wrapper class, it must accept whatever kind of wrapped class.


    > I vaguely
    > remember standard saying something about ignoring cv-qualification at
    > such places, but can not find it from real thing. From standpoint of
    > style i would deconstify it anyway even if it was legal.


    Yeah, perhaps better do that.

    I still wonder if above is formally correct, though... :)


    Cheers,

    - Alf
     
    Alf P. Steinbach, May 4, 2010
    #3
    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. Replies:
    11
    Views:
    1,109
  2. Javier
    Replies:
    2
    Views:
    568
    James Kanze
    Sep 4, 2007
  3. 0m
    Replies:
    26
    Views:
    1,124
    Tim Rentsch
    Nov 10, 2008
  4. fungus
    Replies:
    13
    Views:
    892
    fungus
    Oct 31, 2008
  5. Replies:
    2
    Views:
    542
    Andrew Koenig
    Feb 9, 2009
Loading...

Share This Page