模板模板参数 [英] Template Template Parameters

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

问题描述

这似乎理解模板模板param会杀了我:(,lemme解释我在我的心中造成困惑我的错误观念:

It seems understanding template template param will kill me :(, lemme explain what misconception i made in my mind which confuses me :

template<class T>
class B {}; // A templated class

这是另一个代码:

template<template<class X> class Z = B> // problem is in this line for me
class BB{}; 

注意模板类BB的参数列表中的行是:

note the line in parameter list of templated class BB , which is :

template<class X> class Z = B



现在我想问的是什么阻止c ++认为Z不是另一个模板类Z:

now what i want to ask is what stops c++ to think that Z is not a another templated class Z i.e :

template<class X> class Z{
}

而不是认为Z类是模板参数本身。

rather than thinking Class Z is templated parameter itself.

感谢很多,我真的很感谢任何帮助,从我的心中消除这种误解)

Thanks a lot, i really appreciate any help to remove this misconception from my mind)

推荐答案

Mankarse已经回答了你的问题,但我认为我会反正。

Mankarse has answered your question, but I thought I'd chime in anyway.

模板模板参数就像正常的模板类型参数,除了它们匹配模板而不是具体类型:

Template template parameters are just like normal template type parameters, except that they match templates instead of concrete types:

// Simple template class
template <typename Type>
class Foo
{
    Type m_member;
};

// Template template class
template <template <typename Type> class TemplateType>
class Bar
{
    TemplateType<int> m_ints;
};

如果它有帮助,你可以把它们当作函数指针。正常函数只接受参数,如普通模板只接受类型。但是,一些函数接受接受参数的函数指针,就像模板模板类型接受接受类型的模板一样:

If it helps, you can kind of think of them as like function pointers. Normal functions just accept arguments like normal templates just accept types. However, some functions accept function pointers which accept arguments, just like template template types accept templates that accept types:

void foo(int x)
{
    cout << x << endl;
}

void bar(void (*f)(int))
{
    f(1);
    f(2);
}

要在注释中回答您的问题:模板模板模板参数是不可能的。然而,他们不可能的原因只是因为标准化委员会决定模板模板是足够的,可能使编译器实现者更容易生活。话虽这么说,没有什么阻止委员会决定他们是可能的,那么这样的事情将是有效的C ++:

To answer your question in the comments: template template template parameters are not possible. However, the reason they are not possible is just because the standardisation committee decided that template templates were enough, probably to make lives easier for the compiler implementors. That being said, there's nothing stopping the committee from deciding that they are possible, then things like this would be valid C++:

template <template <template <typename> class> class TemplateTemplateType>
class Baz
{
    TemplateTemplateType<Foo> m_foos;
};

typedef Baz<Bar> Example;
// Example would then have Bar<Foo> m_foos;
// which would have Foo<int> m_ints;

同样,您可以在函数指针中看到。

Again, you can see parallels in function pointers.

                      types <=> values
                  templates <=> functions of values
         template templates <=> functions of functions of values
template template templates <=> functions of functions of functions of values

Baz 将是:

void baz(void (*g)(void (*f)(int)))
{
    g(foo);
}

您将在哪里使用模板模板模板?

Where would you use a template template template?

这是非常牵强,但我可以想到一个例子:一个真正的通用图搜索库。

It's pretty far-fetched but I can think of one example: a really generic graph searching library.

图搜索中的两个常见的算法是深度优先搜索(DFS)和广度优先搜索(BFS)。这两种算法的实现是相同的,除了一方面:DFS使用一堆节点,而BFS使用队列。理想情况下,我们只需编写一次算法,将栈/队列作为参数。此外,我们想要指定堆栈或队列的实现容器,这样我们可以这样做:

Two common algorithms in graph searching are the depth-first search (DFS) and the breadth-first search (BFS). The implementation of the two algorithms is identical except in one regard: DFS uses a stack of nodes whereas BFS uses a queue. Ideally, we'd just write the algorithm once, with the stack/queue as an argument. Also, we'd want to specify the implementation container of the stack or queue, so that we could do something like:

search<Stack, Vector>( myGraph ); // DFS
search<Queue, Deque>( myGraph ); // BFS

但是什么是堆栈或队列?好吧,就像在STL中一样,栈或队列可以用任何种类的容器实现:vectors,deques,lists等,也可以是任何元素类型的栈,所以我们的栈或队列将有接口: p>

But what is a Stack or a Queue? Well, just like in the STL a stack or a queue can be implemented with any kind of container: vectors, deques, lists etc. and could also be stacks of any element type, so our stacks or queues would have the interface:

Stack<Vector, int> // stack of ints, using a vector implementation
Queue<Deque, bool> // queue of bools, using a deque implementation

向量 Deque 本身就是模板类型!

But Vector and Deque themselves are template types!

将是一个模板模板,如:

So finally, our Stack would be a template template like:

template <template <typename> class Storage, typename Element>
struct Stack
{
    void push(const Element& e) { m_storage.push_back(e); }
    void pop() { m_storage.pop_back(); }
    Storage<Element> m_storage;
};

然后我们的 search 作为模板模板模板!

And our search algorithm would then have to be a template template template!

template <template <template <typename> class, typename> class DataStructure,
          template <typename> class Storage,
          typename Graph>
void search(const Graph& g)
{
    DataStructure<Storage, typename Graph::Node> data;
    // do algorithm
}

记住:模板模板不是合法的C ++,所以这个整个图搜索的东西实际上不会编译。这只是一个如果? :)

Remember: template template templates are not legal C++, so this whole graph search thing won't actually compile. It's just a "what if?" :)

这篇关于模板模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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