0

After listening to various CppCon YT talks on modern C++ tips and best-practices I'm sorta confused -
Specifically, is this construct bad for performance by disabling the compiler's ability to use move semantics for CDerived?

class CDerived : public CBase   {

public:

           CDerived();
  virtual ~CDerived() = default;   // <== THIS - Bad, even when using default?
    
    
  virtual void  DoStuff();
};

I suppose in contrast to other questions linked in comments this comes down to

  • Does the default destructor count as auto-generated or user-declared destructor (given it's created by the compiler).
ATV
  • 4,116
  • 3
  • 23
  • 42
  • 1
    Does this answer your question? [Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator?](https://stackoverflow.com/questions/4943958/conditions-for-automatic-generation-of-default-copy-move-ctor-and-copy-move-assi) – mch Feb 23 '23 at 09:47
  • It's hard to answer within the context you provide. What are "some YT talks"? Please post links or at least explain your objections. Did you measure or profile anything? – Friedrich Feb 23 '23 at 09:47
  • 1
    It has happened that "some YT talks" are inaccurate. Perhaps the `virtual` is the problem, and not the `default`? – BoP Feb 23 '23 at 09:47
  • 2
    Adding the destructor removes the auto-generated move constructor. So, yes, objects of this class cannot be moved. When you remove the destructor, it can be moved. When specifying the destructor, you also have to specify move constructor and move assignment operator. – mch Feb 23 '23 at 09:48
  • @Friedrich Sorry, should have been more specific - I'd have to search through recent CPPCON talks for hours to find the relevant bits; but basically it sounds like modern C++ guidelines suggest not to provide dummy (empty) destructors in derived classes. Unsure if this applies to `default` as well. – ATV Feb 23 '23 at 09:49
  • @mch ..does it apply regardless of providing an empty destructor or having the compiler synthesize one via `default`? – ATV Feb 23 '23 at 09:51
  • @mch Question is does `default` fall under **auto-generated** or **user-declared** – ATV Feb 23 '23 at 09:57
  • 3
    when you write `~CDerived = default;`, it is user-declared, because you declared it to be default. – mch Feb 23 '23 at 09:58
  • @mch Hmm... *"The destructor is not user-provided (meaning, it is either implicitly declared, or explicitly defined as defaulted on its first declaration)"*, https://en.cppreference.com/w/cpp/language/destructor – ATV Feb 23 '23 at 10:05
  • 1
    and it is explicitly defined as defaulted in your case. – mch Feb 23 '23 at 10:13
  • why would you declare this destructor at all? I mean in the presence of public inheritance it does matter how the destructor of `CBase` is delcared/defined, and if the destructor of `CDerived` does nothing anyhow, why bother to declare it? – 463035818_is_not_an_ai Feb 23 '23 at 12:27
  • @463035818_is_not_a_number ..cleaning up old legacy code, earlier misconceptions of C++. This is *old* code... – ATV Feb 23 '23 at 14:42
  • @ATV `= default` is as *old* as move semantics – 463035818_is_not_an_ai Feb 23 '23 at 16:11

0 Answers0