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 33,227 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: Templated convertion operator   
   04 Oct 13 09:57:21   
   
   From: daniel.kruegler@googlemail.com   
      
   Am 04.10.2013 10:19, schrieb demo:   
   > I have been using a templated convertion operator in a project. When   
   > porting this to visual studio 2012, some of it stopped   
   > compiling. Should the following code compile on a C++11-compiler?   
      
   I'm pretty sure, it shouldn't and basically all compilers I have access   
   to do reject your code.   
      
   > I   
   > don't have any other compilers to test it with, if someone could check   
   > what Clang does, I would be happy.   
      
   Clang also rejects it.   
      
   > #include    
   >   
   > class foo   
   > {   
   > public:   
   > 	template   
   > 	operator T()   
   > 	{   
   > 		return T();   
   > 	}   
   > };   
   >   
   > int main(int argc, char* argv[])   
   > {	   
   > 	foo f;   
   > 	static_cast(f);   
   > }   
   >   
   >   
   > The static_cast fails with   
   >   
   > 1>main.cpp(17): error C2440: 'static_cast' : cannot convert from 'foo'   
   > to 'std::string'   
   > 1> No constructor could take the source type, or constructor overload   
   > resolution was ambiguous   
   >   
   > When casting to an int or another UDT it works fine.   
      
   I assume that your "other UDT" must be a type that does not have any   
   converting constructors, otherwise I would expect it to cause an   
   ambiguity as well.   
      
   > Any ideas?   
      
   In your example you are direct-initializing a class type with several   
   single-argument constructors by a type that provides a conversion   
   function template (The static_cast here refers to   
   direct-initialization). The compiler attempts to find the best matching   
   constructor but all of them are equally valid and none is better than   
   the other, because there exists a successful conversion function   
   template deduction for each of those constructors.   
      
   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