Bogdan
Bogdan

Reputation: 137

Specifying one of base class template parameters by derived class template parameter. How to use base class's using declarations

For example, I have the next code:

template<typename T, typename T2>
class a
{
public:
    using myfl_t = float;

    a(myfl_t fl, double db) {}

    virtual void f()
    {
        std::cout << "a::f()\n";
    }
};

template<typename T>
class b : public a<T, int>
{
    using base_t = a<T, int>;
    using myfl_t = typename base_t::myfl_t;
public:
    b(myfl_t fl, double db)
        : base_t(fl, db) {}

    void f() override
    {
        std::cout << "b::f()\n";
    }
};

It seems that I can reuse the base class's myfl_t declaration only by duplicating using in the derived class. Is there an easier way to do it?

Upvotes: 3

Views: 232

Answers (2)

shargors
shargors

Reputation: 2157

myflt_t defined in class a is not visible in class b as explained here: https://eli.thegreenplace.net/2012/02/06/dependent-name-lookup-for-c-templates

You can bring myflt_t into class b by without the explicit re-declaration:

using typename base_t::myfl_t

Upvotes: 1

Nikos C.
Nikos C.

Reputation: 51910

You can drop the using in b and use this in the constructor:

b(typename a<T, int>::myfl_t fl, double db)

In any event, your current code seems fine to me. You aren't really duplicating anything here. Duplicating would be if you had to specify float again, which is not the case here, so it's fine.

See this for the reason why:

Why is the keyword "typename" needed before qualified dependent names, and not before qualified independent names?

Upvotes: 0

Related Questions