template non-type arguments

H

Hicham Mouline

hi,
can a non-type argument of a template be of float type?
If no, how can one get same intended result?
rds,
 
M

Michael DOUBEZ

Hicham Mouline a écrit :
hi,
can a non-type argument of a template be of float type?
no.

If no, how can one get same intended result?

I don't know the result you want :)
If you want to parametrize a type with a float value, then you cannot
unless you wrap it in a parametrized class.

template<int ID>
struct ParamFloat;

template<>
struct ParamFloat<0>
{
static const float value=0.0;
};

template<>
struct ParamFloat<1>
{
static const float value=3.14116;
};

//...

Then

struct<class FLOAT>
struct mult2
{
static float value=FLOAT::value*2f;
};


If you want to use or compute a float value (computing series ...) then
you just embed the value:

template<unsigned N>
struct slow_pi
{
static const double value= sqrt2<N-1>::value
+ ((N%2)?1.0:-1.0)/(2N+1);
};

template<>
struct slow_pi<0>
{
static const double value =1;
};

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top