Trying to extract template parameter value in the following code:
template<std::size_t SIZE>
class Foo {};
template <template<std::size_t> class T, std::size_t K>
auto extractSize(const T<K>&) {
return K;
}
int main() {
Foo<6> f1;
Foo<13> f2;
std::cout << extractSize(f1) << std::endl;
std::cout << extractSize(f2) << std::endl;
}
(As an answer for the question: Extract C++ template parameters).
However, is there a way to do it without knowing the type of the template parameter. Something like (code below doesn't compile...):
template <template<class SIZE_TYPE> class T, SIZE_TYPE K>
auto extractSize(const T<K>&) {
return K;
}
The compilation error on the above, is:
error: unknown type name 'SIZE_TYPE'
template <template<class SIZE_TYPE> class T, SIZE_TYPE K>
^