Re: typedefs in class templates and their (non)inheritance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux