vendredi 27 mars 2020

Deduce type of reference template parameter

If one has a

template <class T>
class A{};

// global namespace, static storage duration
static constexpr A<int> a;

is it possible to deduce the type A<int> by passing a as a reference template param such as:

// This question asks to make the syntax in the following line compile:
static_assert(std::is_same<A<int>, typename GetReferenceType<a>::type>::value, "");
// I am aware the next line works, but it's not what I am looking for in this question, since
// I not only want to deduce `A<int>` but also `int`
static_assert(std::is_same<A<int>, decltype(a)>::value, "");

// This is pseudo code of how this could work in theory
template <const T& RefT, class T> // I know this does not compile, but this shows what I want
struct GetReferenceType{          // which is automatically deduce type `T` without having to 
  using type = T;                 // write it out
};

This should also work, but does not fulfill the above syntax requirement:

template <class T>
constexpr auto GetReferenceTypeFunc(const T& t) -> T;

static_assert(std::is_same<A<int>, decltype(GetReferenceTypeFunc(a))>::value, "");

Why I want to do this

Imagine a does not have a short type A<int> but instead something like A<OmgWhyIsThisTypeNameSoLong>.

Then, if you want to instantiate a type with A<OmgWhyIsThisTypeNameSoLong>, you'd have to write:

Instantiate<A<OmgWhyIsThisTypeNameSoLong>>;

It just so happens that we already have global object a, so it would be nice not to have to write that long type but instead Instantiate<a>.

There is of course the option of creating an alias using AOmg = A<OmgWhyIsThisTypeNameSoLong> but I would really like to get around spamming the namespace with another very similar name to A.

Aucun commentaire:

Enregistrer un commentaire