[英]template template parameter of unknown type
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 ). (作为问题的答案: 提取C ++模板参数 )。
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;
}
Compilation error: 编译错误:
error: unknown type name 'SIZE_TYPE'
template <template<class SIZE_TYPE> class T, SIZE_TYPE K>
^
auto
to the rescue! auto
救援!
template <template<auto> class T, auto K>
auto extractSize(const T<K>&) {
return K;
}
That way the type of the value you passed in as template parameter is automatically inferred. 这样,您自动推断作为模板参数传递的值的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.