C++11: defaulted and deleted functions

C++ programmers seem to (or at times have to) get along with the current syntax and semantics of the four special member functions since 1983, or so we think. However, library designers often come across stumbling blocks and annoyances that are a direct result of these rules.

trying out online

I am using XCode 4.2 & LLVM-3.0. LLVM & GCC seem to be on equal footing in terms of implementation of various C++11 language features. You can try out clang-llvm on windows as well. Perhaps I will post seperately on how to build and use clang-llvm on Windows.

<p style="text-align: justify;">
  OTOH you can try out <a href="http://llvm.org/demo/index.cgi">llvm on web</a> as well.
</p>

<p style="text-align: justify;">
  </div> </blockquote> 

  <p style="text-align: justify;">
    By default, C++ implicitly declares the following four member functions for every class:
  </p>

  <ul style="text-align: justify;">
    <li>
      default constructor (ctor)
    </li>
    <li>
      copy constructor (copy ctor)
    </li>
    <li>
      operator = (assignment operator)
    </li>
    <li>
      destructor (dtor)
    </li>
  </ul>

  <p style="text-align: justify;">

  </p>

  <p style="text-align: justify;">
    In addition, a default global implementation of the following operators are implemented for each class:
  </p>

  <ul style="text-align: justify;">
    <li>
      address-of operator <tt>&</tt>
    </li>
    <li>
      indirection operator <tt>*</tt>
    </li>
    <li>
      member access operator <tt>-></tt>
    </li>
    <li>
      member indirection operator <tt>->*</tt>
    </li>
    <li>
      free-store allocation <tt>operator</tt> <tt>new</tt>
    </li>
    <li>
      free-store deallocation <tt>operator</tt> <tt>delete</tt>
    </li>
  </ul>

  <p style="text-align: justify;">
    In both cases, users may override the defaults by providing an explicit declaration of these special member functions and operators in a given class. These default functions have been more trouble than use with most of the libraries, where you want control over how the object is allocated, copied or deleted.
  </p>

  <p style="text-align: justify;">
    The latest <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2210.html#&quot;default&quot;">&#8220;Defaulted and Deleted Functions&#8221; proposal</a> introduces two new syntactic mechanisms. The first mechanism reintroduces the default implementation of a member function or an operator. The second mechanism deletes the definition of the currently visible function.
  </p>

  <h2 style="text-align: justify;">
    default specifier
  </h2>

  <pre class="brush: cpp; title: ; notranslate" title="">

class Sample { public: // //  the pack of four functions that compiler //  implements by default for each class // Sample() = default; Sample(const Sample&) = default; ~Sample() = default; Sample& operator=(const Sample&) = default; };

>
>

> Sample class declares all the four-special functions to be implemented by default. Actually we do not need to put default specifier with each method as compiler will provide you anyway but they are good way of documenting your code. default specifier can be applied to function definitions as well rather than declarations: >

>
>
class Sample
{
public:
    //
    //  the pack of four functions that compiler
    //  implements by default for each class
    //
    Sample();
    Sample(const Sample&) = default;
    virtual ~Sample();
    Sample& operator=(const Sample&) = default;
};
//
//
//  implementation file for Sample
//
Sample::Sample() = default;
Sample::~Sample() = default;
>
>

> default specifier is not restricted to just member functions but can be used on free-standing functions as well. The only thing to remember is that not to specify default on both declaration and definition. >

>
>

> delete specifier >

>
>

> delete specifier is more interesting than default specifier, cause it can solve some of the annoying issues while writing general frameworks or libraries. One of the nice things is that it is declarative that you don’t want certain function to be implemented: >

>
>
class Sample
{
public:
    //
    //  the pack of four functions that compiler
    //  implements by default for each class
    //
    Sample() = default;
    virtual ~Sample() = default;
    //
    //
    //  I don’t want objects of this class to be
    //  copied or assigned
    //
    Sample(const Sample&) = delete;
    Sample& operator=(const Sample&) = delete;
    //
    //
    //  custom logic to copy objects of this class
    //
    virtual Sample* Clone() const = 0;
};
>
>

> Similar to default you can specify delete on definitions (with XCode, clang-llvm 3.0, I am running into some issues, but this should not be a problem moving forward.) in your implementation files. >

>
>

> Summary >

>
>

> I liked the idea that you can declaratively control the generation of special functions and operators for classes. This is big win as it will reduce the cryptic code and will document the intent very well. All in all this is very useful feature. >

>
> >
> >
>
> >
>
>

>

>