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

  • Thread starter Alf P. Steinbach
  • Start date
A

Alf P. Steinbach

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
 
Ö

Öö Tiib

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.
 
A

Alf P. Steinbach

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
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top