Solved

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

Posted on 2003-11-04
6
608 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 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 30

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now