public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* typedefs in class templates and their (non)inheritance
@ 2009-08-05 10:55 alexhuang(黃崇龍)
  2009-08-05 11:47 ` John (Eljay) Love-Jensen
  0 siblings, 1 reply; 2+ messages in thread
From: alexhuang(黃崇龍) @ 2009-08-05 10:55 UTC (permalink / raw)
  To: gcc-help

Are typedefs in class templates required to be redeclared in descendents?

The following code does not compile using gcc 4.2.2
-----------------------------------
template <typename T>
struct MyFunctor : public std::unary_function<int, T>
{
    result_type operator()(argument_type arg)
    {
        return arg;
    }
};

MyFunctor<int> foo;
-----------------------------------
error: ISO C++ forbids declaration of ‘result_type’ with no type

The compiler says that result_type and argument_type are not defined.  They are defined in std::unary_function, but apparently not inherited.  There is a thread http://gcc.gnu.org/ml/gcc-bugs/1997-10/msg00045.html which explains that these typedefs cannot be inherited because std::unary_function may be specialized on <int, int> and the typedefs might not exist in that specialization.  Can't the compiler discover whether or not the specialization exists and hence whether or not the typedefs exist?  Redeclaring typedefs seems a bit error-prone to me -- plus, I'm lazy.

The following code does compile using gcc 4.2.2
-----------------------------------
template <typename T>
struct MyFunctor : public std::unary_function<int, T>
{
    typedef typename std::unary_function<int, T>::result_type result_type;
    typedef typename std::unary_function<int, T>::argument_type argument_type;

    result_type operator()(argument_type arg)
    {
        return arg;
    }
};

MyFunctor<int> foo;
-----------------------------------

Am I missing some other construct that will make std::unary_function's typedefs visible in MyFunctor?

Thanks,
Alex

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: typedefs in class templates and their (non)inheritance
  2009-08-05 10:55 typedefs in class templates and their (non)inheritance alexhuang(黃崇龍)
@ 2009-08-05 11:47 ` John (Eljay) Love-Jensen
  0 siblings, 0 replies; 2+ messages in thread
From: John (Eljay) Love-Jensen @ 2009-08-05 11:47 UTC (permalink / raw)
  To: alexhuang  (黃崇龍), GCC-help

Hi Alex,

> Are typedefs in class templates required to be redeclared in descendents?

Required?  No.

You can do this:

struct MyFunctor
  :
  public std::unary_function<int, T>
{
  typename std::unary_function<int, T>::result_type operator()(
    typename std::unary_function<int, T>::argument_type  arg)
  {
    return arg;
  }
};

> The following code does not compile using gcc 4.2.2 [snip]

Correct, it should not compile, it is not C++ (ISO 14882) compliant.

Any compiler which compiles that code is non-compliant.

The GCC maintainers have been working hard to get GCC compliant with ISO
14882.  I don't know how many known discrepancies are left, but I suspect
there aren't many.

> Can't the compiler discover whether or not the specialization
> exists and hence whether or not the typedefs exist?

No, it can't make that discovery, because that different information may be
available at the time that MyFunctor is instantiated, and due to look up
resolution may (otherwise) have different behavior which would violate ODR.

> Redeclaring typedefs seems a bit error-prone to me...

You don't have to redeclare with typedefs.

But I would, as you have done in your example, since (to me) the alternative
(above) is more error-prone since it is harder to read.  I try to write my
code to favor the poor maintenance programmer.  (Which may very well be
myself.)

> -- plus, I'm lazy.

:-)

> Am I missing some other construct that will make std::unary_function's
> typedefs visible in MyFunctor?

Other than the above alternative to avoid typedefs and MyFunctor and to use
the typedefs in std::unary_function itself... none that I can think of.

Sincerely,
--Eljay


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-08-05 11:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-05 10:55 typedefs in class templates and their (non)inheritance alexhuang(黃崇龍)
2009-08-05 11:47 ` John (Eljay) Love-Jensen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).