Infer earlier template parameters when later parameter given

  c++, template-argument-deduction, templates

Given

template <typename S, typename T>
T make_T(S const &s) { ... }

How can I infer S while explicitly providing T?

I would like to be able to say:

auto t = make_T<auto, int>(S{});

but clang and gcc tell me that auto is not allowed in template argument.
Had the arguments happened to be reversed in the prototype of make_T,
then all would be well;
I could explicitly give T and leave S to be inferred.

In a previous question,
the proposed solution was to declare a helper function that reversed the arguments, e.g.,

template <typename T, typename S>
T make_T_reversed(S const &s) { return make_T<S,T>(s); }

which now enables

auto t = make_T_reversed<int>(S{});

as desired, but I’m hoping there might be a more direct way that doesn’t require creating temporary helper functions. I’m asking as a new question because the accepted answer of the previous question doesn’t answer my actual question:
is there a direct means of achieving this?
I’m feeling hopeful that with C++17 and C++20 (not around at the time of the previous question), there may now be, but I’ve sadly been unable to find it.

Source: Windows Questions C++

LEAVE A COMMENT