Explicitly default and remote constructor: is there any similar functionality available in VS2012?

In VS2012, the function "Explicitly default and remote function of special members" ( http://en.wikipedia.org/wiki/C++0x#Explicitly_defaulted_and_deleted_special_member_functions , http://www.open-std.org/jtc1/sc22/wg21/ docs / papers / 2007 / n2346.htm ) is not yet available ( http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx ). Is there any workaround for using such functions, even if it is very verbose? In practice, I can translate this

struct NonCopyable { NonCopyable() = default; NonCopyable(const NonCopyable&) = delete; NonCopyable & operator=(const NonCopyable&) = delete; }; 

for something with the same functionality, but without using default and delete ? How?

+4
source share
2 answers

You are right, these functions are not yet available.

However, you can do this:

 struct NonCopyable { // ... private: NonCopyable(const NonCopyable&); NonCopyable & operator=(const NonCopyable&); }; 

By simply declaring copy-constructor and operator-assign -ment-operator (no definition) as private, you make them unusable. So this is the effect you want.

Good answer here: fooobar.com/questions/95200 / ...

+4
source

It seems that you want to create a class that cannot be copied. C ++ 11 implements delete functions that can easily provide this functionality, and in pre-C ++ 11 you can achieve the same by:

  • Declare the copy constructor and copy assignment operator as private and
  • Do not specify definitions for both.

Good reading:
Non-copyable mixin

+2
source

Source: https://habr.com/ru/post/1501139/


All Articles