开发者

Deleted and Defaulted Functions Real World Examples

开发者 https://www.devze.com 2023-04-01 09:43 出处:网络
I was looking at the C++11 new features and one of them confuses me, because I can\'t fi开发者_JS百科gure out a way to use it in Real World.

I was looking at the C++11 new features and one of them confuses me, because I can't fi开发者_JS百科gure out a way to use it in Real World.

It's Deleted and Defaulted Functions, does anyone have real world examples of it's usage or is it just one of those features that just adds some sugar?


A user-declared special member function is not trivial. If a class has any nontrivial special member functions, the class is not POD. Therefore, this type is POD:

struct S {
    S() = default;
    S(int) { }
};

but this type is not POD:

struct S {
    S() { }
    S(int) { }
};


struct A
{
  A(const A& arg) : data(arg.data)
  {
    do_something_special();
  }

  // by writing copy constructor, we suppress the generation of
  // implicit default constructor A::A()

  int data;
};

void foo1()
{
  A a; // does not work, since there's no default constructor
}

Let's say that our default constructor does not do anything special and is (more or less) equal to the compiler generated one. We can fix it by either writing our own default constructor (which can get tedious if our class has many non-static members), or by using the = default syntax:

struct A
{
  A() = default;
  A(const A& arg) : data(arg.data)
  {
    do_something_special();
  }

  int data;
};

Deleting functions is useful when we want to forbid using specific overloads or template specializations, or just for forbidding copying (or moving) objects.

void foo(char c) {}
void foo(int i) = delete; // do not allow implicit int -> char conversion

When you want to forbid copying (i.e. thread objects), the usual idiomatic way is to declare private copy constructor without implementation (yeah, or use boost::noncopyable). While this works for most cases, you can get into some obscure linker errors sometimes. Consider:

struct A
{
  A() = default;

  friend void foo();

private:
  A(const A&);
};

void foo()
{
  A a;
  A b(a); // results in linker error in gcc
}

Making A(const A&) deleted, we avoid potential linker errors and make our intent (disallow copying) very clear.


You would use the deleted functions for classes where you want to prevent copying or direct instantiation for example (a la singleton where you want to do a get_instance() function instead). You cold also use delete to prevent certain variations of your constructor.

The default is useful if you want the compiler generated constructor for any of the ones implicitly generated. For example if you create a custom argument constructor, the default no argument one would not be generated by the compiler so you could ask that it be generated for you with the default keyword.

See here for examples of the above

http://www2.research.att.com/~bs/C++0xFAQ.html#default

0

精彩评论

暂无评论...
验证码 换一张
取 消