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 33,336 of 33,346    |
|    =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All    |
|    Re: Template conversion operator special    |
|    18 May 14 03:35:01    |
   
   From: daniel.kruegler@googlemail.com   
      
   Am 18.05.2014 02:55, schrieb TS:   
   > I have some problem regarding template conversion operator on MSVC.   
   >   
   > class Test {   
   > public:   
   > template    
   > operator T() const;   
   > operator int() {   
   > return 4;   
   > }   
   > };   
   >   
   > template<>   
   > Test::operator int() const {   
   > return 3;   
   > }   
   >   
   > The above code compiles fine with GCC but is rejected by MSVC:   
   >   
   > error C2910: 'Test::operator int' : cannot be explicitly specialized   
      
   This seems odd and looks like a compiler error to me. Interestingly the   
   following variant of your code is accepted by MSVC:   
      
   struct Test2 {   
    template T f() const;   
    int f() { return 4; }   
   };   
      
   template<> int Test2::f() const { return 3; }   
      
   > However, I do need to keep both template and non-template version of   
   > operator int() for a particular reason. Any ideas?   
      
   A possible workaround is to use a helper template as follows:   
      
   class Test {   
    template   
    T conv_func_impl() const;   
   public:   
    template    
    operator T() const {   
    return conv_func_impl();   
    }   
    operator int() {   
    return 4;   
    }   
   };   
      
   template<> int Test::conv_func_impl() const { return 3; }   
      
   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)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca