Conversion operator not inherited?

S

SpOiLeR

Hello everybody...

Following code doesn't work:

class char_convertible
{
public:
char_convertible () : el('t') {}
char_convertible (char arg) : el(arg) {}
operator char () const { return el; }

private:
char el;
};

template <class tclass, typename tname>
class simple_template
{
public:
simple_template (tname arg) : el (arg) {}
virtual ~simple_template() {};

operator tclass() const {return el;}

protected:
tclass el;
};


class simple_inherited : public simple_template<char_convertible,char>
{
public:
simple_inherited (char arg) :
simple_template<char_convertible,char> (arg) {}

operator char_convertible () const { return el; }
};


int main (void)
{
simple_inherited inh ('z');

char chr;

g = t; // Problem line

return 1;
}

In main, compiler complains about line

g = t;

saying "t" is not convertible to char. Why is that? There is operator in
simple_inherited that allows conversion into char_convertible, which is on
the other hand convertible into char. So, it seems it should work. And,
yes, I know that it will work if I add

operator char () const;

into interface of either simple_template or simple_inherited...

TIA...
 
V

Victor Bazarov

SpOiLeR said:
Following code doesn't work:

class char_convertible
{
public:
char_convertible () : el('t') {}
char_convertible (char arg) : el(arg) {}
operator char () const { return el; }

private:
char el;
};

template <class tclass, typename tname>
class simple_template
{
public:
simple_template (tname arg) : el (arg) {}
virtual ~simple_template() {};

operator tclass() const {return el;}

protected:
tclass el;
};


class simple_inherited : public simple_template<char_convertible,char>
{
public:
simple_inherited (char arg) :
simple_template<char_convertible,char> (arg) {}

operator char_convertible () const { return el; }
};


int main (void)
{
simple_inherited inh ('z');

char chr;

g = t; // Problem line

return 1;
}

In main, compiler complains about line

g = t;

saying "t" is not convertible to char. Why is that? There is operator in
simple_inherited that allows conversion into char_convertible, which is on
the other hand convertible into char. So, it seems it should work. And,
yes, I know that it will work if I add

operator char () const;

into interface of either simple_template or simple_inherited...

TIA...

When resolving overloaded operators (and your situation calls for the
assignment operator resolution), only one user-defined conversion is
taken into consideration. If the code requires more than one user-
defined conversion to be applied, the compiler won't find it, AFAIK.

V
 
S

SpOiLeR

When resolving overloaded operators (and your situation calls for the
assignment operator resolution), only one user-defined conversion is
taken into consideration. If the code requires more than one user-
defined conversion to be applied, the compiler won't find it, AFAIK.

V

That seems like a language flaw to me... I mean, it forces adding
conversion operator into interface in which it already (in a way) exists.
Well, no harm is done adding it if interface is supposed to suport that
conversion, but it seems like an overkill. Do you happen to know what is
rationale behind it? Or maybe an example which would show danger of having
this kind of "chain" of user-defined conversions?
 
S

Samee Zahur

Indeed ... my version of g++ doesn't even seem to be supporting all of
it. But then again, since HE says it can be done - the standard says it
can be done ... I'm sure he knows the standard :)

Samee
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top