Home > other >  Obtain using definition from templated class
Obtain using definition from templated class

Time:01-11

Given the following classes:

// Some random class
class A { };

// A templated class with a using value in it.
template<class TYPE_B> 
class B {
  public:
    using TYPE = TYPE_B;
};

Next we use these two classes in class C. But if we are using B as the template parameter we would like to obtain the TYPE defined in it.

template<class TYPE_C>
class C {
    // A check to see if we have a class of type B 
    static constexpr bool IS_B = std::is_same<B<int32_t>, TYPE_C>::value ||
                                 std::is_same<B<int64_t>, TYPE_C>::value;

  public:
    // This is what not works. How to get B::TYPE here?
    using TYPE = std::conditional<IS_B, TYPE_C::TYPE, TYPE_C>;
};

Class C would we used like:

C<A> ca;
C<B<int32_t>> cb32;
C<B<int64_t>> cb64;

I am compiling this in GCC. My fear what I would like not have to do is to use the std::is_same statement for each type used with B. Put that in the std::conditional. Are there any alternatives?

CodePudding user response:

You have two issues in your code:

  1. You are missing a typename before TYPE_C::TYPE. Since TYPE_C::TYPE is a dependent name, you need to use typename to tell the compiler that you are looking for a type.

  2. You cannot use TYPE_C::TYPE ins the std::conditional1 expression because when TYPE_C is not B<>, that expression is invalid, but will still be evaluated.

One simple solution is to use an intermediate template to access the type, using template specialization, e.g.

template <class T>
struct get_C_type {
    using type = T;
};

template <class T>
struct get_C_type<B<T>> {
    using type = T;
};

template<class TYPE_C>
class C {
public:
    // you still need a typename here
    using TYPE = typename get_C_type<TYPE_C>::type;
};

1 You probably want to use std::conditional_t or std::conditional<>::type here.

  •  Tags:  
  • Related