How do i create unique types like that of boost files?

I've been studying metaprogramming and reviewing many documents and code regarding boost files. What I've been trying to understand and accomplish is creating various types using typedef, template and struct, similar to that of the boost files themselves, to create comparators. Ultimately, I want to be able to create a type that has sub-properties which can be further compareded and classified.

I apologize in advance as I cannot provide any working attempt at this. I am in the struggle.

Informal example:

create type
typedef int coordinate[3];

create standards - "typeless prototypes"
1
2
3
static const coordinate _x = {1,0,0};
static const coordinate _y = {0,1,0};
static const coordinate _z = {0,0,1};


We "now have" three variables under one unique type. Now I want to wrap this further to work with other types (int, float, etc.).

create special cases
1
2
3
4
5
6
7
template<class ty, coordinate c> struct coord_type;
typedef coord_type<int,x> x_i;
typedef coord_type<float,x> x_f;
typedef coord_type<int,y> y_i;
typedef coord_type<float,y> y_f;
typedef coord_type<int,z> z_i;
typedef coord_type<float,z> z_f;


declare more standards
1
2
3
4
5
6
const x_i i_i;
const x_f i_f;
const y_i j_i;
const y_f j_f;
const z_i k_i;
const z_f k_f;

Where other variables can be represented based off this i,j,k standard and written in a more direct vector form.

And so on...




I don't know how helpful this is to those of you who are more experienced with this sort of style, but what I essintially would like to do is have x,y,z variables act as standards for an i,j,k system that classify arbitrary variables of the coordinate type: Is the arbitrary variable i, j, or k? Is it defined as an int or float type?

Where arguments such as sum_y = var1 + var2; are valid, provided:
sum_y is outputted as 6j, and var1 and var2 are 2j and 4j, respectively.


Please don't over-analyze what I have provided. Just focus on the concept I am bringing forward. I know that the examples above are rough, but I myself don't know all the ins and outs for how to approach this.

Thank you for your time. All help and suggestions are appreciated.
You only create new types (classifications) with class. typedef just creates synonyms.

The language type system (that controls initialisation, conversion, overloading, ...) only works on type, not synonyms.
Just guessing - are you looking for something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <complex>

template< int _x, int _y, int _z > struct coordinate_values
{
    static constexpr int x = _x ;
    static constexpr int y = _y ;
    static constexpr int z = _z ;
};

template< typename T, typename C > struct coordinate_type
{
    static const T x ;
    static const T y ;
    static const T z ;

    static T sum() ;
};

template< typename T, typename C > const T coordinate_type<T,C>::x { C::x };
template< typename T, typename C > const T coordinate_type<T,C>::y { C::y };
template< typename T, typename C > const T coordinate_type<T,C>::z { C::z };
template< typename T, typename C > T coordinate_type<T,C>::sum() { return x+y+z ; }

int main()
{
    typedef coordinate_values<1,0,0> _x ;
    typedef coordinate_values<0,1,0> _y ;
    typedef coordinate_values<0,0,1> _z ;

    typedef coordinate_type< int, _x > x_i ;
    typedef coordinate_type< int, _y > y_i ;
    typedef coordinate_type< float, _z > x_f ;

    typedef coordinate_type< std::complex<double>, coordinate_values<10,20,30> > c ;
    std::cout << c::x << '+' << c::y << '+' << c::z
               << " == " << c::sum() << '\n' ;

    return x_i::x + y_i::x + int(x_f::z) ;
}


http://liveworkspace.org/code/42vbJt$0
kbw, when you say 'class', do you mean struct? I'm unaware of the differences between the two when it comes to metaprogramming style. When they are used, I just think: structs have a public default which must be why they are chosen over classes.

JLBorges, that seems pretty close; although, I haven't had a chance to test it and mess around with the piece of code you provided.

I think the following link may help further demonstrate my idea. It is a boost example that shows almost exactly what it is I am trying to accomplish. Except, I don't plan to develop a system to the extent that they do.

http://www.boost.org/doc/libs/1_53_0/doc/html/boost_units/Units.html#boost_units.Units.base_units
Last edited on
So... might anyone have any ideas?
The reason C++ programmers prefer either class or struct is just preference.
The reason old code tends to use struct us because it was written in C.
The reason beginners tend to use one or the other is because that's how their professor taught them.

Generally though, when you hear "class" you should think of both keywords.
Last edited on
A good source for template meta-programming and policy based design is Modern C++ Design by Andrei Alexandrescu.
I'll see if I can get a hold of that source you provided.

Lastly, is this style of program still alive? The reason I ask this is because I hardly ever hear people talk of it, or see anyone write code in that manner. I hope this is something that is useful and that im not wasting my time.
I think it's more of something done in C than C++. For instance:
1
2
3
4
typedef struct tagMyType
{
    //...
} MyType, *PMyType;
Great. Thanks for the help.
Topic archived. No new replies allowed.