Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Inheriting from 'T const' is -- OK or not?

Reply
Thread Tools

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

 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-04-2010
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
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      05-04-2010
On 4 mai, 23:52, "Alf P. Steinbach" <al...@start.no> 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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-04-2010
On 05.05.2010 00:02, * Öö Tiib:
> On 4 mai, 23:52, "Alf P. Steinbach"<al...@start.no> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Inheriting class with private constructor Michael Carr ASP .Net 1 12-29-2003 04:59 AM
Inheriting System.UI.ControlCollection for a WebControl - FindControl doesn't work??? Harry F. Harrison ASP .Net 0 12-18-2003 06:28 PM
Inheriting from base form Andy Breward ASP .Net 1 11-07-2003 06:15 AM
inheriting from a class that inherits from the UserControl class Joel Barsotti ASP .Net 4 10-30-2003 08:05 PM
Inheriting local variables from a common source? Wayne J ASP .Net 2 10-24-2003 05:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57