Problem with template class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • alessandro.salomone@gmail.com

    #1

    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::exce ption)
    {
    if(value < min || value max)
    {
    throw std::argument_e xception("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<unsigne d int, 1, UINT_MAX, true, truemyVal2;

    it works without errors.

    Why?

  • Victor Bazarov

    #2
    Re: Problem with template class

    alessandro.salo mone@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::exce ption)
    {
    if(value < min || value max)
    {
    throw std::argument_e xception("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<unsigne d 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


    Comment

    Working...