是否可以设置std :: tr1 :: tuple的默认值? [英] is it possible to set the default value of std::tr1::tuple?

查看:344
本文介绍了是否可以设置std :: tr1 :: tuple的默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我使用Visual C +++ 2010)
假设我已经定义了一个这样的元组:

(i'm using Visual C+++ 2010) suppose i have defined a tuple like this:

typedef std::tr1::tuple<
int      //i want to set its default value to 9
, double //i want to set its default value to 3.3
, int    //i want to set its default value to 2
, double //i want to set its default value to -7.2
> Mytuple; 

我可以在一个结构体。但我想知道是否可以在 std :: tr1 :: tuple

此外,我想知道当我使用 std :: tr1:tuple struct?

i can do that in a struct. but i wonder if it is possible to do that in std::tr1::tuple.
Besides, i want to know when shoud i use std::tr1:tuple or struct?

可以帮助我吗?

推荐答案

A tuple 结构。他们的目的是非常不同的。结构首先是一个语言构造。 tuple 是一个构造。

A tuple is not a magical replacement for a struct. Their purposes are very different. A struct is, first and foremost, a language construct. A tuple is a library construct.

Structs有默认值,因为语言说你可以编写构造函数给它们默认值。然后语言负责调用该构造函数来初始化结构体。 tuple 作为构造,没有这样的功能,您可以给予 std :: vector< T& 默认 T 表示它将始终自动使用。您可以为每个成员提供初始值,但不能向其提供默认值

Structs get to have default values because the language says that you can write constructors to give them default values. The language then takes responsibility to call that constructor to initialize the struct. tuple, as a library construct, has no such capabilities, anymore than you can give std::vector<T> a default T that it will always automatically use. You can provide initial values for each member, but you can't give it defaults.

如果您考虑过, t想要。想象一下,如果有人可以说每个 tuple< int,float> 总是 用3和54.221创建。即使一些其他代码创建它不知道这个规则,它必须遵循,(就像类型的构造函数使用的所有类型使用)。记住:每个 tuple< int,float> 是相同的类型。

And if you think about it, you wouldn't want to. Imagine if someone could say that every tuple<int, float> was always created with 3 and 54.221. Even if some other code created it that knew nothing about this rule, it would have to be followed, (just as constructors for the type are used everywhere that type is used). Remember: every tuple<int, float> is the same type.

真的, tuple 是一种替代无法对结构体执行反射和对其成员执行编译期迭代。它们存在的另一个主要原因是能够具有编译时动态结构(即,基于编译时参数创建类型的聚合,而不是直接写入文件的静态列表)。

Really, a tuple is a substitute for the inability to perform reflection on a struct and do compile-time iteration over its members. The other main reason they exist is to be able to have compile-time dynamic structures (that is, the ability to create aggregates of types based on compile-time arguments, rather than a static list directly written into a file).

所以,除非你需要使用 std :: tie (有效地返回多个值),迭代成员一个对象的每个成员的一些模板函数),或一些类似的专门的代码,你应该使用一个结构。

So unless you need to use std::tie (for effectively returning multiple values), iteration over members (ie: call some template function for each member of an object), or some similar specialized code, you should be using a struct.

这篇关于是否可以设置std :: tr1 :: tuple的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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