从外部数据文件接收模板争论? [英] receiving template arguements from an external data file?

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

问题描述

这里只是一些理论,如果我正在读取数据文件(如.txt文件),是否有一种方法可以接收对象类型作为参数传递给模板函数?像这样的东西:

示例类类型为a,b和c,它们都继承自类d

data.txt:

Just some theory here, if i was reading from a data file, like a .txt file, is there a way to recieve an object type to pass to a template function as an argument? Something like this:

example class types are a, b, and c, and they all inherit from class d

data.txt:

a b a a c b a b



main.cpp:
...



main.cpp:
...

/* yeh, this is just plain wrong, but this is generally what i want*/

vector<d> vectorArray;
ifstream stream;
stream.open("data.txt");
char type;

while()
{
    type = stream.get();
    if (type == EOF) break;
    create<type>(vectorArray);
};


...

然后在主要功能之外类似

...


...

and then outside of the main function something like

...

template<type>
void create(vectorArray & vector)
{
    d * newObject = new type; // d used as polymorphic class
    vector.push_back(newObject);
}
...



那些/type/pre块是由网页而不是我添加的:P
[修复-SA]



those /type /pre blocks are being added by the web page, not by me :P
[Fixed — SA]

推荐答案

很抱歉,您错过了.不,这是一个错误的方向.关键是:C ++模板是纯静态的.在运行时开始之前,通过编译将所有类型(所有模板参数)实例化为具体类型.

运行时开始后,为时已晚-动态类型在C ++中不存在.

您应该从一个简单的事实开始:在运行时,该进程拥有固定数量的类型,每种类型都被完全实例化;因此在运行时没有模板类型.

现在,在静态类型的语言中,所有基于流的内容(对于序列化很重要的功能)对类型进行修改的方法都基于一组静态已知的标记.对于某些类型的语言,可以使用RTTI或Reflection来构造类型,而在某些语言中,可以使用虚拟构造函数或虚拟静态方法,在C ++中(不允许同时使用虚拟构造函数或虚拟静态方法)使用工厂方法.您需要创建一个工厂,该工厂根据从流中读取的描述符来决定要创建哪种类型的实例.但是,可能的(非模板)类型集应该是静态的,并且在运行时会完全定义.为了使用多态方法,所有这些类型都可以从某个基本类型派生.

听起来有点无聊?我无能为力我在不同的系统中基于多态性和反射创建了一些持久性系统,并且在理论上可能达到了最高的抽象水平,所以我希望我知道这些方法的局限性.

—SA
Sorry to say: you missed. No, this is a wrong direction. The thing is: C++ templates are purely static. All types (all template parameters) are instantiated into concrete types by compilation, before run-time starts.

After run-time starts, it''s too late — dynamic types do not exist in C++.

You should start from a simple given fact: at the moment of run time, the process holds a fixed number of types, each type is fully instantiated; so there are no template types in runtime.

Now, in statically-typed languages, all approaches with modification of the type based on the content of the stream (which is a feature important for serialization) are based on a statically known set of tags. For construction of the types RTTI or Reflection can be used, in some languages virtual constructors or virtual static methods, in C++ (where both virtual constructors or virtual static methods are not allowed) factory methods are used. You need to create a factory which decides an instance of what type to create based on the descriptor read from the stream. Nevertheless, the set of possible (non-template) types should be static and fully defined at the moment of runtime. All those types could be derived from some base type in order to use polymorphous approach.

Sounds a bit boring? Nothing I can do. I created a number of persistence systems based on polymorphism and Reflection in different systems and probably achieved the highest level of abstraction theoretically possible, so I hope I know the limits of such approaches as I used to push them.

—SA


这篇关于从外部数据文件接收模板争论?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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