home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c++.moderated      Moderated discussion of C++ superhackery      33,346 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 32,524 of 33,346   
   Ivan Godard to All   
   How to default an undefined operation?   
   05 Sep 12 00:48:44   
   
   From: igodard@pacbell.net   
      
   I have a template class taking a typename T argument, and need to define   
   a differenceType representing the abstract "distance" between any pair   
   of values. The template is instantiated with enums, built-in types, and   
   random user classes that define operator-(T, T). This difference type   
   was initially declared as:   
   	typedef typeof(T() - T()) differenceType;   
      
   I recently converted an existing enum to a C++11 "enum class", and the   
   above broke because operator- was not defined for enum classes and there   
   is no conversion to something that defines it.   
      
   I want to leave the above for everything that it works for, and for   
   everything else (including enum classes) use the signed type of the same   
   size as the argument. I tried:   
      
   template   
   class       helper {   
   public:   
       typedef   
       typename   
       int		// simplified fromsomething that gets the right sized   
                   // signed type   
                   differenceType;   
       };   
      
   template<>   
   template   
   class       helper {   
   public:   
       typedef   
       typeof(U() - U())   
                   differenceType;   
       };   
      
   and then in my template wrote:   
      
   template   
   class   element :   
       typedef   
       typename   
       helper::differenceType   
               differenceType;	   
       };   
      
   which is used as:   
       enum class foo {a,b,c};   
       element bar;   
      
   The intent here is that "helper" will first match the   
   specialization as the more restrictive definition, but that will fail   
   because operator- is not defined for foo. Failing that, it will then try   
   the full template which will succeed by defining differenceType as int.   
      
   Except it doesn't work - I still get the "operator- is not defined"   
   diagnostic (g++ 4.6.3). So two questions: why doesn't the above work?   
   And if it is not supposed to work, how do I achieve the intended   
   definition of differenceType?   
      
   Ivan   
      
      
   --   
         [ 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)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca