(Re)命名为std :: pair成员 [英] (Re)named std::pair members
问题描述
我不想写 town->第一个
,而是写下 town-> name
。内联命名访问器(重命名地图迭代器的第一个和第二个和命名std ::对成员)是我迄今为止找到的最好的解决方案。我的命名访问器的问题是类型安全的损失:
pair< int,double>
可以指 struct {int index;双值; }
或 struct {int population; double avg_temp; }
。任何人都可以提出一个简单的方法,或许是类似traits的东西?
Instead of writing town->first
I would like to write town->name
. Inline named accessors (Renaming first and second of a map iterator and Named std::pair members) are the best solutions I have found so far. My problem with named accessors is the loss of type safety:
pair<int,double>
may refer to struct { int index; double value; }
or to struct { int population; double avg_temp; }
. Can anyone propose a simple approach, perhaps something similar to traits?
我经常想从一个函数返回一个对或一个元组,类型如 struct city {string name; int zipcode; }
和它的ctor。我很高兴了解boost和C ++ 0x,但我需要一个纯C ++ 03解决方案,而不增强。
I often want to return a pair or a tuple from a function and it is quite tiring to introduce a new type like struct city { string name; int zipcode; }
and its ctor every time. I am thrilled to learn about boost and C++0x but I need a pure C++03 solution without boost.
更新
Re andrewdski的问题:是,一个(假设的)语法如 pair< int = index,double = value>
从 pair< int = population,double = avg_temp>
创建不同类型将满足您的要求。我甚至不介意必须实现一个自定义的对/元组模板类ONCE,并只是传递一个'名称traits'模板参数适当地,当我需要一个新的类型。我不知道'名称特征'会是什么样子。也许不可能。
Re andrewdski's question: yes, a (hypothetical) syntax like pair<int=index, double=value>
which would create a distinct type from pair<int=population, double=avg_temp>
would meet your requirement. I do not even mind having to implement a custom pair/tuple template class ONCE and just passing a 'name traits' template argument to it approprietly when I need a new type. I have no idea how that 'name traits' would look like. Maybe it's impossible.
推荐答案
我看不到你能做得比
struct city { string name; int zipcode; };
没有什么非必要的。你需要两个成员的类型,你的整个问题是给两个成员的名字,你希望它是一个独特的类型。
There's nothing non-essential there. You need the types of the two members, your whole question is predicated around giving names to the two members, and you want it to be a unique type.
你知道关于聚合初始化语法,对吧?你不需要一个构造函数或析构函数,编译器提供的函数很好。
You do know about aggregate initialization syntax, right? You don't need a constructor or destructor, the compiler-provided ones are just fine.
Example: http://ideone.com/IPCuw
要求您引入新类型,否则 pair< string,int>
在(name,zipcode)和(population,temp)之间是不明确的。
Type safety requires that you introduce new types, otherwise pair<string, int>
is ambiguous between (name, zipcode) and (population, temp).
在C ++ 03中,返回一个新的元组需要:
In C++03, returning a new tuple requires either:
city retval = { "name", zipcode };
return retval;
或写一个方便的构造函数:
or writing a convenience constructor:
city::city( std::string newName, int newZip ) : name(newName), zipcode(newZip) {}
获取
return city("name", zipcode);
但是,使用C ++ 0x,您将可以写
With C++0x, however, you will be allowed to write
return { "name", zipcode };
且不需要用户定义的构造函数。
and no user-defined constructor is necessary.
这篇关于(Re)命名为std :: pair成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!