pseyfert
pseyfert

Reputation: 3613

aliassing a template parameter pack

I would like to use a using alias for a parameter pack such that the templates can be used elsewhere in the code base. In the following code I commented the lines where I would use the types:

template <typename FirstArg, typename ... OtherArgs>
struct Base {
    using UsableFirstArg = FirstArg;
// this would be needed later
//    using UsableOtherArgs... = OtherArgs...;
    virtual OtherClass method(OtherArgs... a) = 0
};

template <typename DerivedOfBaseInstantiation>
struct CallerStructure {
// to be used here
//    OtherClass unknownCall(typename DerivedOfBaseInstantiation::UsableOtherArgs... args) {
//        DerivedOfBaseInstantiation instance;
//        return instance.method(args...);
//    }
}

At code write time of CallerStructure, the arguments of unknownCall are not known and are determined by the instantiation of CallerStructure where DerivedOfBaseInstantiation is a type that's derived of Base. In a more complete example that would look like that:

class OtherClass {
};

template <typename FirstArg, typename ... OtherArgs>
struct Base {
    using UsableFirstArg = FirstArg;
    using UsableOtherArgs... = OtherArgs...;

    virtual OtherClass method(OtherArgs... a) = 0;

};

struct Derived_df : Base<int, double, float> {
    OtherClass someMethod(Base::UsableFirstArg);  // int
    OtherClass method(double, float) override ;
};



template <typename DerivedOfBaseInstantiation>
struct CallerStructure {
    OtherClass knownCall(typename DerivedOfBaseInstantiation::UsableFirstArg a) {
        DerivedOfBaseInstantiation instance;
        return instance.someMethod(a);
    }
    OtherClass unknownCall(typename DerivedOfBaseInstantiation::UsableOtherArgs... args) {
        DerivedOfBaseInstantiation instance;
        return instance.method(args...);
    }
};


void instantiations() {
    CallerStructure<Derived_df> c;
    [[maybe_unused]] auto a = c.knownCall(42);
    [[maybe_unused]]  auto b = c.unknownCall(23., 11.f);
}

Any hints of how to access the variadic templates of Base for the method interface in CallerStructure?

mandatory compiler-explorer link

Upvotes: 1

Views: 135

Answers (1)

Jarod42
Jarod42

Reputation: 217275

You cannot alias variadic template argument. You can wrap them in std::tuple:

template <typename FirstArg, typename ... OtherArgs>
struct Base {
    using UsableFirstArg = FirstArg;
    using UsableOtherArgs = std::tuple<OtherArgs...>;

    virtual OtherClass method(OtherArgs... a) = 0;
};

Unwrapping need some helpers:

template <typename DerivedOfBaseInstantiation,
          typename = typename DerivedOfBaseInstantiation::UsableOtherArgs>
struct CallerStructure;

template <typename DerivedOfBaseInstantiation, typename ... OtherArgs>
struct CallerStructure<DerivedOfBaseInstantiation, std::tuple<OthersArgs...>> {
    OtherClass knownCall(typename DerivedOfBaseInstantiation::UsableFirstArg a) {
        DerivedOfBaseInstantiation instance;
        return instance.someMethod(a);
    }
    OtherClass unknownCall(OtherArgs... args) {
        DerivedOfBaseInstantiation instance;
        return instance.method(args...);
    }
};

Upvotes: 1

Related Questions