help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: always true warning


From: Bernd Strieder
Subject: Re: always true warning
Date: Fri, 22 Dec 2006 15:49:30 +0100
User-agent: KNode/0.10.1

Hello,

Mehturt@gmail.com wrote:

> Bernd Strieder wrote:
>> The clean way means (partially) specializing your template, and
>> remove the comparisions from the code for unsigned. If your template
>> itself is too large, you could perhaps forward the member functions
>> with those comparision to other template functions, which you could
>> specialize more easily.
> I was thinking about this, but is it possible at all without having
> specialize for all types? (char, int, long, long long)

You could define one version of the template for the signed types and
another one for the unsigned types, and forward through another
template, which needs only one-liners. You will have to specialize for
all the types, but you can minimize their implementations, like:

------------------

template <typename S> 
void signed_version(const S&);

template <typename S> 
void signed_version(const S& s)
{
  // tons of code
  if (s < 0 )
    //...
}

template <typename U> 
void unsigned_version(const U&);

template <typename U> 
void unsigned_version(const U& s)
{
  // tons of code
}

template <typename T>
void general_version(const T&);

template <>
void general_version<unsigned int>(const unsigned int &);

template <>
void general_version<unsigned int>(const unsigned int &i)
{
   unsigned_version<unsigned int)(i); // forward
}

template <>
void general_version<unsigned long>(const unsigned long &);

template <>
void general_version<unsigned long>(const unsigned long &i)
{
   unsigned_version<unsigned long)(i); // forward
}

// more specializations of general_version for signed and unsigned types

template <typename Integer>
struct fuzzy
{

   void method(const Integer& i)
        {
                general_version(i); // invoke the dispatcher on signedness
        }

};

// No need to specialize template class fuzzy, if only one method needs
special treatment.

--------------

I think I would prefer the warning in your case, if there are no other
reasons for the template hackery, then it should not be done. If you
have the same problem in more instances, then creating a custom traits
class might be helpful, i.e. make general_version to a class and put
the things depending on the integer type into it as typedefs or
whatever, just extending the idea. Maybe you could use existing traits
classes, like those from boost, I think they include signedness.

I'm probably not reachable until next year, so I have to hope there are
competent people to help you still reading, if you have more questions.

Bernd Strieder




reply via email to

[Prev in Thread] Current Thread [Next in Thread]