Вопрос

I'm just starting learning C++11 and I never saw this syntax in the list of new features:

template <template <typename> class F>
struct fun;

what is it and how does it work?

Это было полезно?

Решение

Note: What you are looking at is an "old" feature, and has been there since long before c++11.


template <template <typename> class F> struct Obj;

In the above Obj is a template only accepting a template-parameter which is also a template[1]; this is most often referred to as a template-template parameter [2].

1) in this specific example it will only accept a template that takes one type-parameter.
2) Link to SO question: Template Template Parameters


Imagine that you'd like to have a wrapper around some class template; you don't care which class template this is as long as you can specify a template argument for it.

If so, you can use a template-template parameter as in the below example:

template<template<typename T> class TemplateType>
struct Obj {
  TemplateType<  int> m1;
  TemplateType<float> m2;
};

template<typename T>
struct SomeTemplate { /* ... */  };

Obj<SomeTemplate> foo;

In the above, foo will be a Obj<SomeTemplate> having two members:

  1. SomeTemplate< int> m1
  2. SomeTemplate<float> m2

Другие советы

This should works in C++98 as well. This is a template as an argument from a template. I mean a template class will expected as the argument for F. Maybe this page will help you: http://www.cprogramming.com/tutorial/templates.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top