Naming pair elements

0 Arend Peter Castelein · September 17, 2015
I'm working on a programming problem and I'm trying to do something along these lines

I have a typedef called fraction

typedef pair<double,pair<int,int>> fraction;
typedef pair fraction;

and I want to access the elements in this way
fraction fr;
fr.value = .5; //same as fr.first
fr.numerator = 1; //same as fr.second.first
fr.denominator = 2; //same as fr.second.second

I believe I've seen this done by working some magic with typedef and typename but I don't remember the specifics

Post a Reply


Oldest  Newest  Rating
0 Arend Peter Castelein · September 25, 2015
Interesting I hadn't heard about unions before.

I ended up doing it like this

#define val first
#define num second.first
#define denom second.second

I'm not sure if it works with union but the think I like about leaving it as a nested pair is I can set the sorting priority of the various elements
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator