8c1c026c   
   From: daniel.kruegler@googlemail.com   
      
   Am 13.04.2012 20:49, schrieb Thiago Adams:   
   > What type "T" or "auto" should I use for i? - C++ 11 allowed   
      
   I don't think that 'auto' is appropriate here, because it would simply   
   select the type of the initializer (which is int here). If I understood   
   your question correctly, you should use a deduction based on decltype:   
      
   > template   
   > bool is_zero(const number& n)   
   > {   
   > for (T i = 0 ; i< n.digits(); i++)   
   > {   
   > if (n[i] != 0)   
   > return false;   
   > }   
   > return true;   
   > }   
      
   To me a natural deduction would be something like   
      
   using T = decltype(n.digits());   
      
   justs before the loop. It reflects the fact, that T would always contain   
   a value that fits into what n.digits() returns. Any comparison like   
      
   if (n[i] != 0)   
      
   looks fine to me, even though one might consider to select the more   
   generic form   
      
   if (n[i])   
      
   > The objective is to have a correct and fast code.   
   >   
   > Mathematically speaking, the value returned by digits() and the value   
   > of i are both positives integers.   
      
   Would you think that e.g.   
      
   using T = decltype(n.digits());   
      
   would have possible disadvantages in this regard?   
      
   > The value returned in digits() can be converted/stored inside a   
   > computer   
   > using some integral type available in the C++ language.   
      
   This sounds as if you would consider return types of n.digits() that are   
   user-defined *and* that very badly choices in a loop. Am I understanding   
   you right?   
      
   > The type used to index operator [](type index) is also some C++   
   > integral type.   
   >   
   > I have control about the "number concept", but I don't want to add   
   > "computer" details.   
   > For instance, I could add to the type the maximum number of digits   
   > allowed.   
   >   
   > struct number {   
   > static const some_integral_type max_digits = some_value;   
   > }   
   > but I don't want to add typedefs like size_t, bits and bytes.   
   > I want to find out the best types based on math requirements,   
   > using traits, decltype etc..   
      
   If you think that in general decltype(n.digits()) is a good choice, but   
   for some types might be bad in this algorithm, you could define a trait   
   with a default value, e.g.   
      
   template   
   struct your_number_traits {   
    using type = decltype(std::declval.digits());   
   };   
      
   then write   
      
   template   
   bool is_zero(const number& n)   
   {   
    using T = typename your_number_traits::type;   
    for (T i = 0 ; i < n.digits(); i++)   
    {   
    if (n[i] != 0)   
    return false;   
    }   
    return true;   
   }   
      
   Now user-code does not need to do anything, unless the default is not   
   the best choice, so they could specialize the trait. The good news are   
   that for most situations, user-code needs not to care about the trait.   
      
   I did not really understand your corresponding discussion about the   
   return type of n[i]. If your question is: I want an optimal comparison   
   against some null concept, you could extend above trait with a second   
   default member, e.g.   
      
   template   
   struct your_number_traits {   
    using type = decltype(std::declval.digits());   
    static bool is_zero(const number& n, const type& i)   
    { return n[i] != 0; }   
   };   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|