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 31,974 of 33,346   
   =?UTF-8?B?RGFuaWVsIEtyw7xnbGVy?= to All   
   Re: What is the purpose of the explicit    
   02 Mar 12 12:48:47   
   
   From: daniel.kruegler@googlemail.com   
      
   On 2012-03-02 11:32, Krzysztof Żelechowski wrote:   
   > 	What is the purpose of the explicit keyword?   
   >   
   > 	To tell the compiler that a certain constructor may not be used to 	   
   > implicitly cast an expression to its class type.   
   >   
   > 	Foo a = 42;   
   >   
   > 	Compile-time error: can't convert 42 to an object of type Foo   
   >   
   > What is implicit about that definition?  The type is explicitly given, and   
   > the definition should be accepted IMHO.   
      
   Originally there did not exist explicit constructors (not to speak of   
   explicit conversion functions) in C++. It turned out that this could   
   lead to a lot of unexpected conversions with unwanted side-effects. To   
   give a class designer a choice, explicit constructors were invented and   
   their interaction rules with expressions. The categories   
   direct-initialization and copy-initialization were separated to allow   
   for discrimination between conversions considered in "explicit" and   
   "non-explicit" conversions. The reason for these choices can be located   
   to the fact, that   
      
   a) the unexpected conversions typically happened when a function was   
   called, e.g. consider   
      
   void bar(Foo);   
      
   and the call context   
      
   int main() {   
     bar(42);   
   }   
      
   b) The semantics of parameter-transfer during a function is call is   
   copy-initialization.   
      
   c) An initialization of the form   
      
   Foo a = 42;   
      
   is copy-initialization, too.   
      
   The latter connection exists for mainly historic reasons: At those old   
   times it turned out to be simpler for implementations to consider that   
      
   Foo get(Foo f) { return f; }   
   Foo a = get(42);   
      
   is equivalent to   
      
   Foo a = 42;   
      
   for some invented function get().   
      
   Unfortunately this equivalence has some unfortunate side-effects,   
   because it enforces that two independent aspects of the language are   
   always going hand in hand:   
      
   a) The wish to control consideration of explicit versus non-explicit   
   conversions.   
   b) If you want to ensure that a conversion is implicit, the expression   
   semantics always *requires* that the destination type is moveable.   
      
   This is really bad, because that means that Foo has to be moveable, even   
   though from a user's perspective the initialization   
      
   Foo a = 42;   
      
   is just the construction of a Foo from an int value not requiring at all   
   a copy or move of a Foo.   
      
   I may have not answered your question, so please elaborate your use-case   
   in more detail in this case. In particular I'm unsure whether you agree   
   with the view that the   
      
   bar(42)   
      
   is an "implicit" conversion.   
      
   In either case, the problem is the equivalence rule mentioned above. If   
   you have a good alternative for this, please present this in more   
   detail. IMO the hardest part is to find wording that specifies a new   
   form of a semantics and to apply this new rule consistently in the   
   standard - If you start with that work you may be surprised how deeply   
   the term copy-initialization impacts the core language. I don't consider   
   "just make my Foo a = 42; well-formed" as a solution. The actual work is   
   to define a new form of semantics - this may turn out to require new   
   syntax, too.   
      
   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