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,683 of 33,346   
   =?UTF-8?B?RGFuaWVsIEtyw7xnbGVy?= to All   
   Re: How to conditionally disable a copy    
   23 Nov 11 08:08:18   
   
   From: daniel.kruegler@googlemail.com   
      
   On 2011-11-23 01:39, Daniel Krügler wrote:   
   > Am 22.11.2011 20:44, schrieb Andrzej Krzemieński:   
   >> I am trying to implement a template "wrapper" for any arbitrary type,   
   >> much like boost::optional. In fact I am trying to write my own   
   >> alternative implementation of optional. I want it to have the   
   >> following property: if the wrapped type T ptovides copy constructor,   
   >> optional also provides a copy constructor; similarly, optional   
   >> provides a move constructor iff T provides one. With concepts (as   
   >> defined in N2914), I would express it like this:   
   >>   
   >> template< typename T>   
   >> struct Optional   
   >> {   
   >> requires CopyConstructible   
   >> Optional( Optional const& );   
   >>   
   >> requires MoveConstructible   
   >> Optional( Optional&& );   
   >> };   
      
      
   [..]   
      
   > If I correctly understand you, Optional does not contain a member or   
   > base of type T (otherwise the compiler-declared constructors would   
   > already work as intended), instead you have some opaque buffer for the   
   > to be constructed T. In this case, I suggest the following approach   
   > (ignoring the copy/move assignment operations for the moment):   
   >   
   > Invent the following empty types:   
   >   
   > struct copy_move1 {};   
   > struct copy_move2 {};   
   >   
   > struct move_no_copy {   
   > move_no_copy(const move_no_copy&) = delete;   
   > move_no_copy(move_no_copy&&) = default;   
   > };   
   >   
   > struct copy_no_move {   
   > copy_no_move(const copy_no_move&) = default;   
   > copy_no_move(copy_no_move&&) = delete;   
   > };   
   >   
   > Define a so-called TransformationTrait (see 20.9.1), name it copy_base,   
   > that has a single template parameter T and that defines a member type   
   > "type" such that   
   >   
   > if std::is_copy_constructible::value == true then type is equal to   
   > copy_move1, else type is equal to move_no_copy.   
   >   
   > This can easily be realized via std::conditional:   
   >   
   > template   
   > struct copy_base : std::conditional<   
   > std::is_copy_constructible::value,   
   > copy_move1, move_no_copy>   
   > {};   
   >   
   > Define a second TransformationTrait, name it move_base, that has a   
   > single template parameter T and that defines a member type "type" such that   
   >   
   > if std::is_move_constructible::value == true then type is equal to   
   > copy_move2, else type is equal to copy_no_move.   
   >   
   > Once you have done so, you start defining Optional like so:   
   >   
   > template< typename T >   
   > struct Optional : private copy_base::type,   
   > private move_base::type   
   > {   
   > };   
   >   
   > The effects should be as if you had a member (or base class) that has   
   > the same copy/move behaviour as T.   
   >   
   > Does this answer your question?   
      
   I just realize that it probably does not. While this approach realized   
   that Optional has the same copy/move construction as type X, it does   
   still not describe how to *define* the corresponding member function   
   *iff* T does have an available member.   
      
   You need to extend my first approach by replacing copy_move1 and   
   copy_move2 by class templates, that implement the copy and move   
   semantics of T acting on the opaque buffer as required by the semantics   
   of Optional. This should still give you the advantage that you don't   
   need to specialize Optional completely.   
      
   - Daniel   
      
      
   --   
         [ 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