接受“任何东西”的模板在C ++中 [英] Templates accepting "anything" in C++

查看:121
本文介绍了接受“任何东西”的模板在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的模板struct将一个字符串与一个值相关联

I have a simple template struct associating a string with a value

template<typename T> struct Field
{
    std::string name; T self;
}



我有一个函数,我想接受一个或多个字段任何类型,并且字段可能是不同类型,因此我使用 std :: initializer_list ,因为根据我的知识,C ++缺少类型化的可变参数,无法确定可变参数的大小,并且必须至少有一个其他参数来确定从何处开始。

I have a function that I want to accept 1-or-more Fields of any type, and the Fields may be of possible different types, so I'm using a std::initializer_list because C++, to my knowledge, lacks typed variadic arguments, cannot determine the size of variadic arguments, and must have at least one other argument to determine where to start.

问题是我不知道如何告诉它接受可能是不同类型的字段。在Java中,我只是使用 foo(Field<?> bar,Field<?> ... baz),但是C ++缺少类型化的可变参数和通配符。我唯一的想法是使参数类型
std :: initializer_list< Field< void *>> ,但是看起来像一个坏的解决方案。 ..有更好的方法吗?

The problem is that I don't know how to tell it to accept Fields that may be of different types. In Java, I would just use foo(Field<?> bar, Field<?>... baz), but C++ lacks both typed variadic arguments and wildcards. My only other idea is to make the parameter of type std::initializer_list<Field<void*>>, but that seems like a bad solution... Is there a better way to do it?

推荐答案

几件事...


  • C ++ 11(你现在讨论的是 std :: initializer_list )具有类型化的可变参数,尤其是命名为可变参数模板

  • C++11 (which you seem to have since you are talking about std::initializer_list) does have typed variadic arguments, in particular they are named variadic templates

Java泛型和C ++模板是完全不同的野兽。 Java泛型创建一个单一类型,存储对 Object 的引用,并提供对接口中的类型的自动转换,但重要的是它执行类型擦除。

Java generics and C++ templates are completely different beasts. Java generics create a single type that stores a reference to Object and provides automatic casting in and out to the types in the interface, but the important bit is that it performs type erasure.

我建议您解释您想解决的问题,并获得解决方案的建议,在C ++中。如果你想真正模仿Java中的行为(我不能坚持足够不同的语言和不同的成语),你可以在C ++中手动使用类型擦除(即使用 boost :: any )。但是我很少觉得需要在程序中进行完全类型擦除...使用变体类型( boost :: variant )更常见。

I would recommend that you explain the problem you want to solve and get suggestions for solutions to your problem that are idiomatic in C++. If you want to really mimic the behavior in Java (which, I cannot insist enough is a different language and has different idioms) you can use type erasure in C++ manually (i.e. use boost::any). But I have very rarely feel the need for full type erasure in a program... using a variant type (boost::variant) is a bit more common.

如果你的编译器支持可变参数模板(不是所有的编译器都支持),你总是可以使用它,但是在后面的可能有点复杂的完全通用的方法,除非您使用类型擦除。 (再次,问题是什么解决?可能有更简单的解决方案...)

If your compiler has support for variadic templates (not all compilers do), you can always play with that, but stashing the fields for later in a vector may be a bit complicated for a fully generic approach unless you use type erasure. (Again, what is the problem to solve? There might be simpler solutions...)

这篇关于接受“任何东西”的模板在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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