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,528 of 33,346   
   Casey Carter to Ivan Godard   
   Re: How to default an undefined operatio   
   05 Sep 12 14:53:58   
   
   From: casey_at_carter_dot_net@nospam.invalid   
      
   On 2012-09-05 02:48, Ivan Godard wrote:   
      
   > 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.   
      
   This is exactly the use case for std::declval(): "creating" an   
   expression of type T for use in non-evaluated contexts - e.g.,   
   decltype(), sizeof() - without needing a valid constructor   
   expression. Try:   
      
       typedef   
         decltype(std::declval() - std::declval()) differenceType;   
      
      
   --   
         [ 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