Solved

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

Posted on 2003-11-04
6
632 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 50 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 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