C++ 如何使用类型列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/901907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use typelists
提问by DaClown
I read about typelists in 'Modern C++ Design' and I understood it as some kind of union for types. By putting diffrent, non-related types in a typelist, one can use it to represent more than one type at once, without inheritance. I tested typelist in some simple functions with primitive types, but I couldn't get any of them to work.
我在“现代 C++ 设计”中读到了类型列表,我将其理解为某种类型的联合。通过将不同的、不相关的类型放在类型列表中,人们可以使用它一次表示一种以上的类型,而无需继承。我在一些带有原始类型的简单函数中测试了 typelist,但我无法让它们中的任何一个工作。
Could someone tell me if my unterstanding of typelists is right and give an simple real world example how to use typelists in every day average code? Thanks in advance.
有人能告诉我我对类型列表的理解是否正确,并给出一个简单的现实世界示例,如何在每天的平均代码中使用类型列表?提前致谢。
Btw, I'm using Windows and Visual Studio 2005 and its compiler.
顺便说一句,我使用的是 Windows 和 Visual Studio 2005 及其编译器。
EDIT: my examples are gone, I use a sandbox project in vs to test those things. But it was quiet similar to code in Dobbs tutorial:
编辑:我的例子不见了,我在 vs 中使用了一个沙箱项目来测试这些东西。但它很安静,类似于 Dobbs 教程中的代码:
void SomeOperation(DocumentItem* p)
{
if (TextArea* pTextArea = dynamic_cast<TextArea*>(p))
{
... operate on a TextArea object ...
}
else if (VectorGraphics* pVectorGraphics =
dynamic_cast<VectorGraphics*>(p))
{
... operate on a VectorGraphics object ...
}
else if (Bitmap* pBitmap = dynamic_cast<Bitmap*>(p))
{
... operate on a Bitmap object ...
}
else
{
throw "Unknown type passed";
}
}
This works but I don't see the advantage over inheritance which is capable to do the same. And dynamic cast don't work on primitive types. Is it possible to use it as a return value like:
这有效,但我没有看到能够做到这一点的继承的优势。动态转换不适用于原始类型。是否可以将其用作返回值,例如:
typedef Typelist<int, string> mylist
mylist myfunction() {
if(foo == bar)
return 5;
return "five";
}
回答by poliklosio
The typelists are generic compile-time collections of types. If you use dynamic_cast, you are missing the point, because it should not be needed, because it is a static, compile time concept.
类型列表是类型的通用编译时集合。如果您使用 dynamic_cast,您就错过了重点,因为它不应该被需要,因为它是一个静态的编译时概念。
This works but I don't see the advantage over inheritance which is capable to do the same.
这有效,但我没有看到能够做到这一点的继承的优势。
You cannot make any existing type inherit from anything you want. This is simply not feasible, because this existing type may be a built in type or a type from a library. Think of the typelists as extensions of lists of types (e.g. in std::pair) for any reasonable number of types (instead of just 2).
你不能让任何现有的类型继承你想要的任何东西。这根本不可行,因为这种现有类型可能是内置类型或来自库的类型。将类型列表视为任何合理数量的类型(而不是仅 2 个)的类型列表(例如在 std::pair 中)的扩展。
The typelists can be used to create a facility to pass around a set of arguments to a function. This is a piece of code that calls generalized functors of 5 parameters (another concept from Modern C++ design) with the arguments supplied in a tupe (yet another one) with the typelist that defines types of objects held in the tuple:
类型列表可用于创建将一组参数传递给函数的工具。这是一段代码,它调用 5 个参数的泛化函子(现代 C++ 设计的另一个概念),参数在一个元组(另一个)中提供,类型列表定义了元组中保存的对象类型:
//functor is just a holder of a pointer to method and a pointer to object to call this
//method on; (in case you are unfamiliar with a concept)
template<class R, class t0, class t1, class t2, class t3, class t4>
R call(Loki::Functor<R,LOKI_TYPELIST_5(t0, t1, t2, t3, t4
)> func,
Loki::Tuple<LOKI_TYPELIST_5(t0, t1, t2, t3, t4)> tuple)
{
///note how you access fields
return func(Loki::Field<0>(tuple), Loki::Field<1>(tuple),
Loki::Field<2>(tuple), Loki::Field<3>(tuple),
Loki::Field<4>(tuple));
}
//this uses the example code
#include<iostream>
using namespace std;
int foo(ostream* c,int h,float z, string s,int g)
{
(*c)<<h<<z<<s<<g<<endl;
return h+1
}
int main(int argc,char**argv)
{
Loki::Functor<int,LOKI_TYPELIST_5(ostream*, int, float, string, int)> f=foo;
//(...)
//pass functor f around
//(...)
//create a set of arguments
Loki::Tuple<LOKI_TYPELIST_5(ostream*, int, float, string, int)> tu;
Field<0>(tu)=&cout;
Field<1>(tu)=5;
Field<2>(tu)=0.9;
Field<3>(tu)=string("blahblah");
Field<4>(tu)=77;
//(...)
//pass tuple tu around, possibly save it in a data structure or make many
//specialized copies of it, or just create a memento of a call, such that
//you can make "undo" in your application; note that without the typelist
//you would need to create a struct type to store any set of arguments;
//(...)
//call functor f with the tuple tu
call(f,tu);
}
Note that only with other concepts like tuples or functors the typelists start to be useful. Also, I have been experiencing Loki for about 2 years in a project and because of the template code (a lot of it) the sizes of executables in DEBUG versions tend to be BIG (my record was 35 MB or so). Also there was a bit of hit on the speed of compilation. Also recall that C++0x is probably going to include some equivalent mechanism. Conclusion: try not to use typelists if you don't have to.
请注意,只有在元组或函子等其他概念中,类型列表才开始有用。此外,我已经在一个项目中体验 Loki 大约 2 年,并且由于模板代码(很多),DEBUG 版本中的可执行文件的大小往往很大(我的记录是 35 MB 左右)。编译速度也受到了一些打击。还记得 C++0x 可能会包含一些等效的机制。结论:如果没有必要,尽量不要使用类型列表。
回答by Paul Hollingsworth
Typelists are a way of passing "lists of parameters" to template meta programs that "execute" as part of the compilation process.
类型列表是一种将“参数列表”传递给作为编译过程一部分“执行”的模板元程序的方式。
As such, they can be used to generate some sort of "union" type, but this is only one possible use.
因此,它们可用于生成某种“联合”类型,但这只是一种可能的用途。
For a "real-world" example: We used typelists as a way to generate the "QueryInterface" method automatically when implementing COM objects in the Cometlibrary.
对于“真实世界”示例:在Comet库中实现 COM 对象时,我们使用类型列表作为自动生成“QueryInterface”方法的一种方式。
It allowed you to write code like this:
它允许您编写如下代码:
class Dog : public implement_qi<make_list<IAnimal, INoisy, IPersistStream> >
{
// The implement_qi template has provided
// an implementation of COM's QueryInterface method for us without
// having to write an ugly ATL "message map" or use any Macros.
...
}
In this example, "make_list" was a template used to generate a "type list", which the implement_qi template could then "enumerate over" to generate the appropriate QueryInterface code.
在此示例中,“make_list”是用于生成“类型列表”的模板,然后implement_qi 模板可以“枚举”以生成适当的QueryInterface 代码。