C ++中模板模板参数的一些用途是什么? [英] What are some uses of template template parameters in C++?

查看:239
本文介绍了C ++中模板模板参数的一些用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些C ++使用模板模板参数(即以模板作为参数的模板)来做基于策略的类设计的例子。这种技术还有什么其他用途?

解决方案

我认为你需要使用模板模板语法来传递一个参数,模板依赖于另一个模板,如下所示:

 模板< template< class>类别H,类别S> 
void f(const H< S& value){
}

这里, H 是一个模板,但我想要这个函数来处理 H 的所有专业化。



注意:我一直在编写c ++多年,只需要一次。我发现这是一个很少需要的功能(当然,当你需要它的时候很方便)。



我一直在想想好的例子, ,大多数时候这不是必要的,但让我们举个例子。让我们假设 std :: vector 不会 typedef value_type



那么你如何编写一个函数,它可以为向量元素创建正确类型的变量?这将工作。

 模板< template< class,class>类V,类T,类A> 
void f(V //这可以是typename V //但是我们假装我们没有它

T temp = v.back();
v.pop_back();
//在temp上做一些工作

std :: cout<<温度< std :: endl;
}

注意:我们 std :: vector 有两个模板参数,type和allocator,所以我们不得不接受它们。幸运的是,由于类型扣除,我们不需要明确写出确切的类型。



你可以这样使用:

  f< std :: vector,int>(v); // v的类型为std :: vector< int>使用任何分配器

或更好的,我们可以使用:

  f(v); //一切都被推断出来,f可以处理任何类型的向量! 

UPDATE :即使这个假设的例子惊人的例子由于c ++ 11引入 auto 。现在相同的函数可以写成:

  template< class Cont> 
void f(Cont& v){

auto temp = v.back();
v.pop_back();
//在temp上做一些工作

std :: cout<<温度< std :: endl;
}

这是我更喜欢写这种类型的代码。 p>

I've seen some examples of C++ using template template parameters (that is templates which take templates as parameters) to do policy-based class design. What other uses does this technique have?

解决方案

I think you need to use template template syntax to pass a parameter whose type is a template dependent on another template like this:

template <template<class> class H, class S>
void f(const H<S> &value) {
}

Here, H is a template, but I wanted this function to deal with all specializations of H.

NOTE: I've been programming c++ for many years and have only needed this once. I find that it is a rarely needed feature (of course handy when you need it!).

I've been trying to think of good examples, and to be honest, most of the time this isn't necessary, but let's contrive an example. Let's pretend that std::vector doesn't have a typedef value_type.

So how would you write a function which can create variables of the right type for the vectors elements? This would work.

template <template<class, class> class V, class T, class A>
void f(V<T, A> &v) {
    // This can be "typename V<T, A>::value_type",
    // but we are pretending we don't have it

    T temp = v.back();
    v.pop_back();
    // Do some work on temp

    std::cout << temp << std::endl;
}

NOTE: we std::vector has two template parameters, type and allocator, so we had to accept both of them. Fortunately, because of type deduction, we won't need to write out the exact type explicitly.

which you can use like this:

f<std::vector, int>(v); // v is of type std::vector<int> using any allocator

or better yet, we can just use:

f(v); // everything is deduced, f can deal with a vector of any type!

UPDATE: Even this contrived example, while illustrative, is no longer an amazing example due to c++11 introducing auto. Now the same function can be written as:

template <class Cont>
void f(Cont &v) {

    auto temp = v.back();
    v.pop_back();
    // Do some work on temp

    std::cout << temp << std::endl;
}

which is how I'd prefer to write this type of code.

这篇关于C ++中模板模板参数的一些用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆