class template specialization with signed/unsigned type problem

Discussion in 'C++' started by andreas.schniertshauer@googlemail.com, Aug 1, 2012.

  1. Guest

    Hello,

    I have a class template of the following form:

    template <typename T, typename F>
    class Converter
    {
    public:
    bool operator () (T& tTo, const F& tFrom) const;
    };

    And I want to do a specialization where F could be a long or unsigned long type:

    template <>
    class Converter<string, F>
    {
    public:
    bool Converter<string, F>::eek:perator () ( string& tTo, const F& tFrom ) const
    {
    static_assert( std::is_integral<F>::value && (std::is_signed<F>::value || std::is_unsigned<F>::value), "Error: type not supported" );

    ...

    if ( std::is_signed<F>::value )
    {
    // do something with long
    }
    else
    if ( std::is_unsigned<F>::value )
    {
    // do something with unsigned long
    }

    ...

    return true;
    }
    };


    Can anyone tell me please how I could write the specialized template class so that I could pass a long or unsigned long? Or isn't this possible to do in one class and I must write two specialized classes one specialization for long and one for unsigned long? I want to have less code, so the second solution would not be my favorite.

    Thanks for the help,
    Andreas.
     
    , Aug 1, 2012
    #1
    1. Advertising

  2. On 8/1/2012 8:33 AM, wrote:
    > Hello,
    >
    > I have a class template of the following form:
    >
    > template <typename T, typename F>
    > class Converter
    > {
    > public:
    > bool operator () (T& tTo, const F& tFrom) const;
    > };
    >
    > And I want to do a specialization where F could be a long or unsigned long type:
    >
    > template <>
    > class Converter<string, F>
    > {
    > public:
    > bool Converter<string, F>::eek:perator () ( string& tTo, const F& tFrom ) const
    > {
    > static_assert( std::is_integral<F>::value && (std::is_signed<F>::value || std::is_unsigned<F>::value), "Error: type not supported" );
    >
    > ...
    >
    > if ( std::is_signed<F>::value )
    > {
    > // do something with long
    > }
    > else
    > if ( std::is_unsigned<F>::value )
    > {
    > // do something with unsigned long
    > }
    >
    > ...
    >
    > return true;
    > }
    > };
    >
    >


    > Can anyone tell me please how I could write the specialized template
    > class so that I could pass a long or unsigned long? Or isn't this
    > possible to do in one class and I must write two specialized classes
    > one specialization for long and one for unsigned long? I want to have
    > less code, so the second solution would not be my favorite.


    I think what you need is a helper function that would *assume* that your
    type is long or unsigned long, and then you need to create two
    specializations, one for 'long', the other for 'unsigned long', and make
    them both call that helper function. That way is much easier and
    cleaner than all those ifs and static_asserts.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 1, 2012
    #2
    1. Advertising

  3. Blonder Guest

    did you mean something like this? Or what did you mean when you say assume?

    template <typename T, typename F>
    void ConverterHelper(T t, F f) {}

    template <typename T>
    void ConverterHelper<>(T t, unsigned long f) {}

    template <typename T>
    void ConverterHelper<>(T t, long f) {}
     
    Blonder, Aug 1, 2012
    #3
  4. On 8/1/2012 10:04 AM, Blonder wrote:
    > did you mean something like this? Or what did you mean when you say assume?
    >
    > template <typename T, typename F>
    > void ConverterHelper(T t, F f) {}
    >
    > template <typename T>
    > void ConverterHelper<>(T t, unsigned long f) {}
    >
    > template <typename T>
    > void ConverterHelper<>(T t, long f) {}
    >


    <sigh> It's difficult to answer without the context. Perhaps you could
    at some point consider quoting the article to which you're replying...

    First off, you had

    template<class T, class F> class C { ...
    bool operator() (T& to, const F& from);
    };

    to specialize it for 'F == long' or 'F == unsigned long' *in a single
    function/class* is rather silly; you end up with lots of 'if' statements
    and/or static asserts. So, don't do it. I suggested to specialize
    twice, but then call some kind of helper class in which you can assume
    that 'long' or 'unsigned long' is the "source" type. BTW, I remember
    submitting something on a similar topic to the FAQ. Have you read the FAQ?

    template<class T> class CH_long_ulong {
    ... // some common functionality
    // maybe in the form of templates
    };

    template<class T> class C<T, long> : CH_long_ulong<T> {
    public:
    bool operator() (T& to, long from) {
    CH_long_ulong<T>::common_part1(from);
    .. // some special processing for 'long'
    CH_long_ulong<T>::common_part2(from);
    .. // some more special processing for 'long'
    }
    };

    template<class T> class C<T, unsigned long>
    : CH_long_ulong<T> {
    public:
    bool operator() (T& to, unsigned long from) {
    CH_long_ulong<T>::common_part1(from);
    .. // some special processing for 'unsigned long'
    CH_long_ulong<T>::common_part2(from);
    .. // some more special processing for 'unsigned long'
    }
    };

    Hope this helps. Ask more questions.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Aug 1, 2012
    #4
    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.
Similar Threads
  1. Bob Hairgrove
    Replies:
    1
    Views:
    418
    John Harrison
    Jun 26, 2004
  2. case2005
    Replies:
    3
    Views:
    1,859
    Nicolas Pavlidis
    Feb 13, 2005
  3. Joseph Turian
    Replies:
    2
    Views:
    492
  4. toton
    Replies:
    1
    Views:
    630
  5. pozz
    Replies:
    12
    Views:
    793
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page