Problem with template class

Discussion in 'C++' started by alessandro.salomone@gmail.com, Nov 8, 2006.

  1. Guest

    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?
     
    , Nov 8, 2006
    #1
    1. Advertising

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

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page