简体   繁体   English

传递带有免费模板参数的模板类型名

[英]Pass template typename with free template argument

In C++11 (or C++) is it possible to pass a template type that is not fully specified. 在C ++ 11(或C ++)中,可以传递未完全指定的模板类型。 Specifically, I want to pass a type that does not have all of its template specifiers defined yet: 具体来说,我想传递尚未定义其所有模板说明符的类型:

template <std::size_t N, typename ARRAYTYPE>
struct A {
  ARRAYTYPE<int, N> int_array;
};

int main() {
  A<10, std::array> my_a;
  return 0;
}

I know that simply redefining ARRAYTYPE = std::array<int, 10> would work, but would not let me utilize an ARRAYTYPE of different size anywhere in A : 我知道简单地重新定义ARRAYTYPE = std::array<int, 10>是可行的,但不会让我在A任何地方使用不同大小的ARRAYTYPE

template <std::size_t N, typename ARRAYTYPE>
struct A {
  ARRAYTYPE<int, N> int_array;
  ARRAYTYPE<int, 1> tiny_int_array;
};

Is this possible? 这可能吗?

It's called a "template template parameter", because it's a template parameter whose value is a template: 之所以称为“模板模板参数”,是因为它是一个模板参数,其值为模板:

template <std::size_t N, template <typename, std::size_t> class ARRAYTYPE>
struct A {
  ARRAYTYPE<int, N> int_array;
  ARRAYTYPE<int, 1> tiny_int_array;
};

That would not be a type, but a template: 那不是一个类型,而是一个模板:

template <std::size_t N, template <typename, std::size_t> class ARRAY_TMPL>
struct A {
   ARRAY_TMPL<int, N> int_array;
};

如果我了解您要完成的工作,则可以将模板本身作为模板template参数传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM