From: daniel.kruegler@googlemail.com   
      
   On 2012-09-05 09: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;   
      
   In the following I will assume that typeof is replaced by decltype,   
   because in the context of standard C++ it is easier to speculate upon   
   standardized language features (But I'm pretty sure that typeof is not   
   the root of your problem, though).   
      
   > 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.   
      
   Correct.   
      
   > 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.   
      
   While this is a possible strategy let me remark that in C++11 you have   
   the type trait underlying_type, which deduces the underlying integral   
   type of any enumeration type.   
      
   > 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.   
      
   Exactly this is what the language dictates, yes.   
      
   > Failing that, it will then try the full template which will succeed   
   > by defining differenceType as int.   
      
   This is too late. At the point where the compiler has selected a   
   specialization, it *has* selected this specialization. If this   
   specialization creates is ill-formed during instantiation, you will   
   get this instantiation error.   
      
   > 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?   
      
   Because once the compiler has selected a specialization, this will be   
   instantiated, irrespective whether this will produce an error or not.   
      
   > And if it is not supposed to work, how do I achieve the intended   
   > definition of differenceType?   
      
   There are several approaches possible. E.g. you could define a helper   
   traits that evaluates to true, if the expression decltype(U() - U())   
   is well-formed. In this case, you use the specialization that computes   
   the difference type from this decltype, otherwise you would fallback   
   to your version that has a default difference type (or uses other   
   means to compute it).   
      
   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)   
|