Jonathan Mee
Given a templatized function like this: `template <typename T> void Func(const T& param)` How can I determine if `param`'s containing template type?
For example, if I call `Func(std::vector<int>())` I'd like to be able to derive the containing template type of `std::vector` from `param`.
Top Answer
Jonathan Mee
You can declare a function to take in and break down an argument. But since you can't return a non-specialized type from a function you can really just use the function to test if `param` matches a template parameter type. That said you can define something like:
template <template <typename> class L, template <typename> class R, typename T>
std::conditional_t<std::is_same_v<L<T>, R<T>>, std::true_type, std::false_type> IsSameContainer(const R<T>&);
If you wanted to perform Substitution Failure Is Not An Error(SFINAE) testing if `param`'s containing template type was a `std::vector` you could define a function something like: `template <typename T> auto Func(const T& param) -> std::enable_if_t<decltype(IsSameContainer<std::vector>(param))::value>`
Alternatively you can use `IsSameContainer` to test if `param`'s containing template type was a `std::vector` with `constexpr if` inside your `Func`:
template <typename T>
void Func(const T& param) {
if constexpr(decltype(IsSameContainer<std::vector>(param))::value) {
// Operate on param knowing it is a std::vector here
}
}
[*Live Example*](http://coliru.stacked-crooked.com/a/49166661297adf87)