# Using defaulted ctor on TObject derived classes?

**URL:** <https://root-forum.cern.ch/t/using-defaulted-ctor-on-tobject-derived-classes/59548>\
**Category:** Newbie\
**Created:** [June 5, 2024, 9:34am UTC](https://root-forum.cern.ch/t/using-defaulted-ctor-on-tobject-derived-classes/59548 "2024-06-05T09:34:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctacke](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ctacke/32/32430_2.png) [@ctacke](https://root-forum.cern.ch/u/ctacke)\
**Post date:** [June 5, 2024, 9:34am UTC](https://root-forum.cern.ch/t/using-defaulted-ctor-on-tobject-derived-classes/59548/1 "2024-06-05T09:34:03Z")

</div>

From the below Pull Request on Github I learned that using `= default;` to create a defaulted ctor for TNamed introduces some issues. I have not fully understood those.

> <https://github.com/root-project/root/pull/15469#discussion_r1626763205>
>
> \`\`\`suggestion
> // The following is \*not\* the default implementation (i.e. do …not use \`= default\`.
> // Default implementation can do 'more' than expected. In this case it reset all the
> // data member of \`TNamed\` and more importantly its base class \`TObject\`.
> // TObject's implementation of the \`IsOnHeap\` bit requires the memory occupied
> // by \`TObject::fUniqueID\` to \*not\* be reset between the execution of \`TObject::operator new\`
> // and the \`TObject\` constructor (Finding the magic pattern there is how we can determine
> // that the object was allocated on the heap.  
> TNamed() : fName(), fTitle() { }
> \`\`\`

This confuses me somewhat:

If I create a normal user defined class that derives from TObject (I hope that’s generally okay?!), should I avoid using `= default;` on the default ctor?

If this is really the case: How can I check for the default ctor being valid in my unit tests? Is there some test helper function in ROOT that helps me check my default ctor?

* * *

_ROOT Version:_ latest  
_Platform:_ any?  
_Compiler:_ any?

* * *

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [June 5, 2024, 10:38am UTC](https://root-forum.cern.ch/t/using-defaulted-ctor-on-tobject-derived-classes/59548/2 "2024-06-05T10:38:30Z")

</div>

There is `magic` code in TObject constructor which allows us to distinguish  
if object created with new operator or as stack variable. In first case  
object should be cleaned up - via ROOT cleanup lists.

All this does not work if `= default` constructor is used.  
Therefore all ROOT classes (especially graphics-related) do not use such constructors declaration.

For your particular case it depends if objects ownership handled properly.  
Means you destroy it, add to TList with ownership flag and so on.  
In such case you can use `= default` constructor.

Probably @pcanal can give better explanation.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [June 5, 2024, 10:24pm UTC](https://root-forum.cern.ch/t/using-defaulted-ctor-on-tobject-derived-classes/59548/3 "2024-06-05T22:24:46Z")

</div>

> [@ctacke](#):
>
> If this is really the case: How can I check for the default ctor being valid in my unit tests? Is there some test helper function in ROOT that helps me check my default ctor?

There is simple test:

```auto
auto obj = new MyType{}
if (! obj->IsOnHeap())
   throw "TObject::IsOnHeap is broken for MyType";
delete obj;

```

---

<div class="post-metadata">

**Author:** ![ctacke](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ctacke/32/32430_2.png) [@ctacke](https://root-forum.cern.ch/u/ctacke)\
**Post date:** [August 13, 2024, 6:32am UTC](https://root-forum.cern.ch/t/using-defaulted-ctor-on-tobject-derived-classes/59548/4 "2024-08-13T06:32:42Z")

</div>

Note:

There’s a new PR on ROOT to improve the documentation:

> <https://github.com/root-project/root/pull/16218>

It’s also discussed, whether one can check for derived classes using this convention.

---

<div class="post-metadata">

**Author:** ![ctacke](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ctacke/32/32430_2.png) [@ctacke](https://root-forum.cern.ch/u/ctacke)\
**Post date:** [November 7, 2024, 3:34pm UTC](https://root-forum.cern.ch/t/using-defaulted-ctor-on-tobject-derived-classes/59548/5 "2024-11-07T15:34:40Z")

</div>

(already asked [here](https://github.com/root-project/root/pull/16218#issuecomment-2288062979), but it seems more appropriate here.)

If I got this right, the `IsOnHeap`-behaviour depends on UB? And not allowing `= default` on derived classes to support this UB is at least unintuitive. (It’s good, that it’s documented, and it’s good to consider checking it!)

Are there any plans to phase out (and then deprecate) this? So that people actually can use `= default`?

The first step towards phasing out would be to document the issues involved when using `= default` and what a non-functional IsOnHeap would actually induce?

---

<div class="post-metadata">

**Author:** ![silverweed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/silverweed/32/36670_2.png) [@silverweed](https://root-forum.cern.ch/u/silverweed)\
**Post date:** [November 7, 2024, 3:52pm UTC](https://root-forum.cern.ch/t/using-defaulted-ctor-on-tobject-derived-classes/59548/6 "2024-11-07T15:52:26Z")

</div>

Hello @ctacke,  
yes, unfortunately the behaviour relies on UB and it’s not easy to get rid of this - in fact, it is likely impossible without breaking lots of code depending on it. The plan for ROOT moving forward is to rely less on `TObject` and automatic memory management in general, especially in user-facing code.

Most of the newer interfaces, such as RDataFrame or RNTuple, don’t require users to interface to `TObject` and we plan for this to be the case for all cases where this is possible. However when it comes to interfacing with old code unfortunately this behavior needs to be accounted for.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [November 7, 2024, 3:58pm UTC](https://root-forum.cern.ch/t/using-defaulted-ctor-on-tobject-derived-classes/59548/7 "2024-11-07T15:58:13Z")

</div>

The major reason we use the `IsOnHeap` feature is to due to the implicit shared ownership. For example for this code to work:

```auto
TFile *file = TFile::Open(filename, "RECREATE");
TTree t("t", ""); // Register itself for shared ownership in TFile.
delete file;

```

the `TFile` needs to know that it can **not** delete the `TTree` as it usually does.

The `IsOnHeap` behavior is already disable on some platform where the UB actually implemented by the compiler/OS actively prevent the `IsOnHeap` detection.

We are moving away from this in the context of the `ROOT v7` interface which does **not** rely on any implicit shared ownership. To avoid disruption on existing code, we are not planning on removing the feature in the old interface but recommend that one migrate to the new interface on their own schedule (once they are available).

In the meantime, there is already very few reasons to write new code that derives from `TObject` (counter examples includes of course code that needs to fit in an existing framework that leverage `TObject` or the `ROOT` collections)

---

<div class="post-metadata">

**Author:** ![ctacke](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ctacke/32/32430_2.png) [@ctacke](https://root-forum.cern.ch/u/ctacke)\
**Post date:** [November 7, 2024, 4:27pm UTC](https://root-forum.cern.ch/t/using-defaulted-ctor-on-tobject-derived-classes/59548/8 "2024-11-07T16:27:12Z")

</div>

> [@silverweed](#):
>
> yes, unfortunately the behaviour relies on UB and it’s not easy to get rid of this - in fact, it is likely impossible without breaking lots of code depending on it.

I fully understand the problems of having to maintain backward compatible code! That’s why I asked about phasing out and not about deprecating or even removing things.

> [@pcanal](#):
>
> The `IsOnHeap` behavior is already disable on some platform where the UB actually implemented by the compiler/OS actively prevent the `IsOnHeap` detection.

Wow! So that means, that there are platforms where it would not make a difference, if our code would use `= default`? Very interesting!

> [@pcanal](#):
>
> We are moving away from this in the context of the `ROOT v7` interface which does **not** rely on any implicit shared ownership.

That’s exactly what I was asking for: A plan to move away from this! Very good.
