issue creating a type in "meta-like" programming

Hello all,

I've been trying to play around with structs, templates, typedefs, and all that other fun stuff that ties into the so called metaprogramming form of writing code.

What I am trying to do is create a type that has 1 typename placeholder and 7 integer placeholders. I would like to create a few constant variables of this type to serve the purpose of a standard for the rest of my code, and also intialize variables that are subject to change. My code does not compile for obvious reasons. I know there is a lot wrong, and I am unsure how to fix some of it.

.h file
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
namespace siu {
	class unit_def;
}

class siu::unit_def {
	const int v_max;
	const int SImax;

	// SI base units
	typedef int _dim[7]; // SImax
	/* base dim */
	static const _dim meter;
	static const _dim gram;
	static const _dim second;
	static const _dim ampere;
	static const _dim kelvin;
	static const _dim mole;
	static const _dim candela;

	template<class T, int u1, int u2, int u3, 
			  int u4, int u5, int u6, int u7> // v_max
	typedef T dim<T,u1,u2,u3,u4,u5,u6,u7>;
	/* types */ //  Ty,  m,  g,  s,  A,  K, mol, cd
	dim meter   = { int, 1,  0,  0,  0,  0,  0,  0 }; // Obviously wrong
public:
	unit_def(): SImax(7) v_max(8);
};


.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "units.h"

using namespace siu;

unit_def::unit_def(): SImax(7) v_max(8) 
	// Error: "siu::unit_def::unit_def()" 
	//         provides no initializer for:
{					
	/* base type */		//  m,  g,  s,  A,  K, mol, cd
	const _dim meter	= { 1,  0,  0,  0,  0,  0,  0 };
	const _dim gram		= { 0,  1,  0,  0,  0,  0,  0 };
	const _dim second	= { 0,  0,  1,  0,  0,  0,  0 };
	const _dim ampere	= { 0,  0,  0,  1,  0,  0,  0 };
	const _dim kelvin	= { 0,  0,  0,  0,  1,  0,  0 };
	const _dim mole		= { 0,  0,  0,  0,  0,  1,  0 };
	const _dim candela	= { 0,  0,  0,  0,  0,  0,  1 };
}


Thanks in advance for all help with my issue. Please, feel free to share any ideas that may make this easier/more understandable. I'm almost certain that my attempt is by no means a very good way of going about doing this. However, I've been told otherwise before.
Last edited on
You may be interested in boost.Units: http://www.boost.org/doc/libs/release/doc/html/boost_units.html in case you need this for practical reasons (and even for self-education, it's good to compare ideas to what's used in the existing solutions)
Thanks Cubbi. I've actually already been all over the boost.Units solution. I am trying to do something similar, but I don't exactly need to specialize as much as what the boost offers. In addition, I feel like writing out my own library would help me to learn quite a bit, and I would also be aware of exactly what I have and how I did it.
Topic archived. No new replies allowed.