By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,572 Members | 959 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,572 IT Pros & Developers. It's quick & easy.

Problem with template class

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.