Problem with template class

  • Thread starter alessandro.salomone
  • Start date
A

alessandro.salomone

I have a problem with a template class I have defined:
-----------------------------------------------------------------------------------------------------------------
template <typename T, T min, T max>
class Setting
{
private:
T* value;
public:
Setting() : value(NULL) {}
~Setting() {delete value;}

const T* get() const throw() {return value;}
void set(const T& value) throw(std::exception)
{
if(value < min || value > max)
{
throw std::argument_exception("Error");
}

this->value = new T(value);
};
};
-----------------------------------------------------------------------
When I try to define this:
Setting<double, 0.0, 1.0> myVal1;

the compiler tells me:
error C2993: 'double' : illegal type for non-type template parameter
'min'
error C2993: 'double' : illegal type for non-type template parameter
'max'

While if I write:
Setting<unsigned int, 1, UINT_MAX, true, true> myVal2;

it works without errors.

Why?
 
V

Victor Bazarov

I have a problem with a template class I have defined:
-----------------------------------------------------------------------------------------------------------------
template <typename T, T min, T max>
class Setting
{
private:
T* value;
public:
Setting() : value(NULL) {}
~Setting() {delete value;}

const T* get() const throw() {return value;}
void set(const T& value) throw(std::exception)
{
if(value < min || value > max)
{
throw std::argument_exception("Error");
}

this->value = new T(value);
};
};
-----------------------------------------------------------------------
When I try to define this:
Setting<double, 0.0, 1.0> myVal1;

the compiler tells me:
error C2993: 'double' : illegal type for non-type template parameter
'min'
error C2993: 'double' : illegal type for non-type template parameter
'max'

While if I write:
Setting<unsigned int, 1, UINT_MAX, true, true> myVal2;

Where did those 'true, true' come from?
it works without errors.

Why?

Why what? Why is it illegal? Because the Standard says so. Why it
works with 'int'? Because the Standard allows integral types for
non-type template arguments. Why is it that way in the Standard?
Because it is possible to implement with integrals and not possible
with floating-point, IIRC. Try searching "non-type template argument
double" in the archives, you'll find more information.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top