Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2003-11-04
6
Medium Priority
?
650 Views
Last Modified: 2008-01-09
Hi,

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?

Thanks.

0
Comment
Question by:Klaren
6 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 9679451
>> 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;

or,

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

Exceter
0
 
LVL 4

Accepted Solution

by:
PhilipNicoletti earned 200 total points
ID: 9682528
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
{
public:

    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;
}
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9683931
Weird... Then why does this work?

std::vector <float> data;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Expert Comment

by:Zoppo
ID: 9685132
>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'

ZOPPO
0
 

Author Comment

by:Klaren
ID: 9685794
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?

Thanks!
Klaren
0
 
LVL 8

Expert Comment

by:Exceter
ID: 9739328
>> 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.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question