extending std::numeric_limits

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

    #1

    extending std::numeric_limits

    I'm writing a template library that models the IEEE754(r) decimal
    arithmetic standard, and I'd like to provide numeric_limits< for my
    templates. The problem is that I'm not sure how to do this.

    If I had a simple (untemplated) class T, it would be easy. But what I
    have instead is a model of a decimal type (call it DecimalType). Most
    of my free functions, operations, etc look like this:

    template <typename DecimalType>
    DecimalType
    operator+ (const DecimalType& lhs, const DecimalType& rhs);

    A DecimalType is then supposed to have a certain interface.

    So what I'd like to do is this:

    namespace std {

    template <typename DecimalType>
    class numeric_limits
    {
    // extensions go here
    };

    }

    But I can't do that because std::numeric_li mits is already defined. So
    I need to be able to provide a partial specialization. But how can I
    do this? (Note: I can see some ways to do it if DecimalType is
    required to derive from an ABC, but I'd prefer not to do this.)

    Thanks,
    - James

  • Victor Bazarov

    #2
    Re: extending std::numeric_li mits

    jamesrjones@gma il.com wrote:
    I'm writing a template library that models the IEEE754(r) decimal
    arithmetic standard, and I'd like to provide numeric_limits< for my
    templates. The problem is that I'm not sure how to do this.
    >
    If I had a simple (untemplated) class T, it would be easy. But what I
    have instead is a model of a decimal type (call it DecimalType). Most
    of my free functions, operations, etc look like this:
    >
    template <typename DecimalType>
    DecimalType
    operator+ (const DecimalType& lhs, const DecimalType& rhs);
    >
    A DecimalType is then supposed to have a certain interface.
    >
    So what I'd like to do is this:
    >
    namespace std {
    >
    template <typename DecimalType>
    class numeric_limits
    {
    // extensions go here
    };
    >
    }
    >
    But I can't do that because std::numeric_li mits is already defined. So
    I need to be able to provide a partial specialization. But how can I
    do this? (Note: I can see some ways to do it if DecimalType is
    required to derive from an ABC, but I'd prefer not to do this.)
    ...
    template<class T, class U, class V>
    class numeric_limits< DecimalType<T,U ,V {
    // extensions go here
    };

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • jamesrjones@gmail.com

      #3
      Re: extending std::numeric_li mits

      On Mar 7, 10:12 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
      >
      ..
      template<class T, class U, class V>
      class numeric_limits< DecimalType<T,U ,V {
      // extensions go here
      >
      };
      >
      V
      This would work fine if DecimalType were a template. But it isn't.
      What I have is this (for example):

      template < class T, class U >
      class fast_model_of_d ecimals {};

      template < class T, class U >
      class small_model_of_ decimals {};

      template < class T, class U >
      class other_model_of_ decimals {};

      I could do this:

      template < class T, class U >
      class numeric_limits< fast_model_of_d ecimals< class T, class U {};

      .... and so on for the other models.

      But what I'd like to do is something like this:

      template <typename T>
      class numeric_limits< T {};

      where T must be a model of a decimal type.

      Note: I'm using BOOST and I'm familiar with enable_if, which works
      great but doesn't quite seem to fit this application. I have a type
      trait template is_decimal_type which returns true if the type is a
      model of a fixed decimal type and false otherwise.

      - James

      Comment

      • Victor Bazarov

        #4
        Re: extending std::numeric_li mits

        jamesrjones@gma il.com wrote:
        On Mar 7, 10:12 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
        >>
        >..
        >template<cla ss T, class U, class V>
        >class numeric_limits< DecimalType<T,U ,V {
        > // extensions go here
        >>
        >};
        >>
        >V
        >
        This would work fine if DecimalType were a template. But it isn't.
        What I have is this (for example):
        >
        template < class T, class U >
        class fast_model_of_d ecimals {};
        >
        template < class T, class U >
        class small_model_of_ decimals {};
        >
        template < class T, class U >
        class other_model_of_ decimals {};
        >
        I could do this:
        >
        template < class T, class U >
        class numeric_limits< fast_model_of_d ecimals< class T, class U {};
        >
        ... and so on for the other models.
        That's what you should (or have to) do.
        But what I'd like to do is something like this:
        >
        template <typename T>
        class numeric_limits< T {};
        >
        where T must be a model of a decimal type.
        You cannot do that because that is not a specialisation.
        Note: I'm using BOOST and I'm familiar with enable_if, which works
        great but doesn't quite seem to fit this application. I have a type
        trait template is_decimal_type which returns true if the type is a
        model of a fixed decimal type and false otherwise.
        Just bite the bullet and specialise it for each of your templates.

        V
        --
        Please remove capital 'A's when replying by e-mail
        I do not respond to top-posted replies, please don't ask


        Comment

        • jamesrjones@gmail.com

          #5
          Re: extending std::numeric_li mits

          On Mar 7, 11:35 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
          You cannot do that because that is not a specialisation.
          Yes, I know.
          Just bite the bullet and specialise it for each of your templates.
          There are a couple of reason why I want to avoid this:

          1. Each specialization will have an identical implementation. (I
          realize I could probably define a macro to keep the code duplication
          to a minimum, but this still seems ugly.)

          2. I'd like to make it easy to extend the library as easily as
          possible. Currently, users can do this by (a) creating a new model and
          (b) providing a new specialization of the type trait template. I can
          add a (c), but the more steps that are required, the harder this is
          for potential users.

          Any creative ideas out there?

          Thanks,
          - James

          Comment

          • mlimber

            #6
            Re: extending std::numeric_li mits

            On Mar 7, 11:50 am, jamesrjo...@gma il.com wrote:
            On Mar 7, 11:35 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
            >
            You cannot do that because that is not a specialisation.
            >
            Yes, I know.
            >
            Just bite the bullet and specialise it for each of your templates.
            >
            There are a couple of reason why I want to avoid this:
            >
            1. Each specialization will have an identical implementation. (I
            realize I could probably define a macro to keep the code duplication
            to a minimum, but this still seems ugly.)
            >
            2. I'd like to make it easy to extend the library as easily as
            possible. Currently, users can do this by (a) creating a new model and
            (b) providing a new specialization of the type trait template. I can
            add a (c), but the more steps that are required, the harder this is
            for potential users.
            >
            Any creative ideas out there?
            Define a single class with the limits you want, and then make your
            other specializations publicly inherit from it.

            Cheers! --M

            Comment

            • Bo Persson

              #7
              Re: extending std::numeric_li mits

              jamesrjones@gma il.com wrote:
              On Mar 7, 11:35 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
              >You cannot do that because that is not a specialisation.
              >
              Yes, I know.
              >
              >Just bite the bullet and specialise it for each of your templates.
              >
              There are a couple of reason why I want to avoid this:
              >
              1. Each specialization will have an identical implementation. (I
              realize I could probably define a macro to keep the code duplication
              to a minimum, but this still seems ugly.)
              Or use a base implementation

              template<>
              std::numeric_li mits<YourType: base_implementa tion<some, parameters>
              { };


              Bo Persson


              Comment

              Working...