base class public type (non template and template base class)

Discussion in 'C++' started by Hicham Mouline, Apr 20, 2009.

  1. Hello,

    This compiles

    #include <iostream>

    class Base {
    public:
    typedef double type;
    };

    class Derived : public Base {
    public:
    void f(type t) const {}
    };


    While this

    template <typename T>
    class Base {
    public:
    typedef double type;
    };

    template <typename T>
    class Derived : public Base<T> {
    public:
    void f(type t) const {} // type has not been declared
    };


    This templated version requires

    template <typename T>
    class Derived : public Base<T> {
    public:
    void f(typename Base<T>::type t) const {}
    };


    Is g++3.4 standard compliant here? Or is this a g++3.4 restriction?

    writing
    typename Base<T>::type
    is tedious?

    I tried to put type in a non template base class B of Base, but in Derived,
    I still needed to qualify B::type

    rds,
    Hicham Mouline, Apr 20, 2009
    #1
    1. Advertising

  2. Hicham Mouline wrote:
    > This compiles
    >
    > #include <iostream>
    >
    > class Base {
    > public:
    > typedef double type;
    > };
    >
    > class Derived : public Base {
    > public:
    > void f(type t) const {}
    > };
    >
    >
    > While this
    >
    > template <typename T>
    > class Base {
    > public:
    > typedef double type;
    > };
    >
    > template <typename T>
    > class Derived : public Base<T> {
    > public:
    > void f(type t) const {} // type has not been declared
    > };
    >
    >
    > This templated version requires
    >
    > template <typename T>
    > class Derived : public Base<T> {
    > public:
    > void f(typename Base<T>::type t) const {}
    > };
    >
    >
    > Is g++3.4 standard compliant here? Or is this a g++3.4 restriction?
    >
    > writing
    > typename Base<T>::type
    > is tedious?
    >
    > I tried to put type in a non template base class B of Base, but in Derived,
    > I still needed to qualify B::type


    FAQ section 35. See "dependent name".

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Apr 20, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page