Sign in to follow this  
D_Tr

Templates and Typedefs

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this