Templates and Typedefs

Started by
4 comments, last by asdfg__12 16 years, 5 months ago
Hello ppl, I 've just written the following small fixed-point class:

template<typename I, Int32 N>
class Fixed
{
public:
	Fixed();
	Fixed(Float64 a_Float64) : value(a_Float64 * Pow(2.0, N))
	{};
	
	operator Float64()
	{
		return (Float64)value * Pow(2.0, -N);
	};
	
	Fixed<I, N> &operator=(Float64 a_Float64)
	{
		value = a_Float64 * Pow(2.0, N);
	};

private:
	I value;
};


where I is the underlying integral type and N is the number of bits to the right of the decimal point. Now, i would like to do some typedefs like the following:

typedef Fixed<UInt64, N> UFixed64<N>;
but the above typedef generates errors (compiled with g++ 4.something):

Fixed.hpp:23: error: ‘N’ was not declared in this scope
Fixed.hpp:23: error: template argument 2 is invalid
Fixed.hpp:23: error: expected initializer before ‘<’ token


Is there any (preferably standard) way to do this kind of typedefs?
Advertisement
Unfortunately, C++ does not include support for templated typedefs (which is what it looks like you're wanting to do).

For more info on this, Google 'C+ templated typedefs' (you might also find some examples of ways you can work around this problem).
Okay, i 'll try google, thanks for the reply :)
Problem is that the template is expecting a number, and not "N" for the template. I suggest creating a macro:

#define UFixed64(N) Fixed<UInt64,N>
You can always enclose the typedefs in a struct!

template< Int32 N >struct FixedType{    typdef Fixed< UInt64, N > U64;    typdef Fixed< UInt32, N > U32;};

Thanks for the tips :). I will write the rest of the class for now and i 'll decide on a solution later.

This topic is closed to new replies.

Advertisement