"ANSI C++ forbids floating-point template arguments" ???


I have a template that is called:
template <class T, T Min, T Max> class Test

I call this template with the following type defs:
typedef Test<unsigned char> Test_byte;
typedef Test<int> Test_int;
typedef Test<float> Test_float;

The compiler does not allow the third typedef: typedef Test<float> Test_float;
It says: "ANSI C++ forbids floating-point template arguments"

I don't understand why this is forbidden! Is there any way around it?
What would be the best solution when you use a template, but you want to use specific values for every instanciated class?

Who can help me out here?


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>> I don't understand why this is forbidden! Is there any way around it?

I don't think it is. After all, you can say,

std::vector <float> data; // Works just fine.

>>template <class T, T Min, T Max> class Test
>> typedef Test<float> Test_float;

Why are you only using one of the three template arguments? Have you tried,

typedef Test <float, 45.0, 45.0> Test_float;


template <class T> class Test
typedef Test<float> Test_float;

The error message is correct.

Section 14.1 paragraph 7 of the ISO C++ Standard :

7 A non-type template-parameter shall not be declared to have floating point, class, or void type. [Example:

template<double d> class X; // error
template<double* pd> class Y; // OK
template<double& rd> class Z; // OK

--end Example]

Based on this, you can probably do this (but I don't know how useful it will be) :

#include <iostream>

using namespace std;

extern const float my_min1 = 2.3f;
extern const float my_max1 = 10.0f;

extern const float my_min2 = -1.0f;
extern const float my_max2 =  1.0f;

template <typename T, const T* MIN, const T* MAX>
class A

    A() : min_allowed(*MIN) , max_allowed(*MAX){}
    T value;
    T min_allowed;
    T max_allowed;

int main()
    A<float,&my_min1,&my_max1> a1;
    A<float,&my_min2,&my_max2> a2;

    cout << a1.min_allowed << " " << a1.max_allowed << endl;
    cout << a2.min_allowed << " " << a2.max_allowed << endl;

    return 0;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Weird... Then why does this work?

std::vector <float> data;
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

>Then why does this work
Because the template paramete is the Type 'double', not a double value.

The problem (as PhilipNicoletti statet) in
template <class T, T Min, T Max> class Test
doesn't come from the 'class T' but from 'T Min' and 'T Max'

KlarenAuthor Commented:
Thank you all for your input.
However, I still don't understand why it isn't allowed to declare a template-parameter to have floating-point type. I understand it is a ISO C++ Standard rule, but WHY?

Doesn't anybody else run into this problem? And although I'm grateful for the solution of PhilipNicoletti, I wonder if there isn't an easier way to introduce some floating point parameters to a template. What do you guys do if you want to use specific values for every instanciated class of  a template?

>> What do you guys do if you want to use specific values for every instanciated class of  a template?

Depending on what you are trying to do you can pass the values through the constructor, use a macro, a global variable, or a local variable.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.