?
Solved

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

Posted on 2003-11-04
6
Medium Priority
?
648 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 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
Technology Partners: 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

718 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