469,569 Members | 1,494 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,569 developers. It's quick & easy.

Problem with template class

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.0myVal1;

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, truemyVal2;

it works without errors.

Why?

Nov 8 '06 #1
1 3930
al*****************@gmail.com wrote:
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.0myVal1;

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, truemyVal2;
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by tyousaf | last post: by
7 posts views Thread by Hendrik Schober | last post: by
7 posts views Thread by Lionel B | last post: by
reply views Thread by Jon Slaughter | last post: by
2 posts views Thread by Sherrie Laraurens | last post: by
7 posts views Thread by StephQ | last post: by
3 posts views Thread by StephQ | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.