# Preprocessor macro to get version of custom class

**URL:** <https://root-forum.cern.ch/t/preprocessor-macro-to-get-version-of-custom-class/52418>\
**Category:** Newbie\
**Created:** [November 17, 2022, 4:20pm UTC](https://root-forum.cern.ch/t/preprocessor-macro-to-get-version-of-custom-class/52418 "2022-11-17T16:20:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![renyhp](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@renyhp](https://root-forum.cern.ch/u/renyhp)\
**Post date:** [November 17, 2022, 4:20pm UTC](https://root-forum.cern.ch/t/preprocessor-macro-to-get-version-of-custom-class/52418/1 "2022-11-17T16:20:50Z")

</div>

Hello,

is there some preprocessor macro that is capable of determining whether a custom class inheriting from `TObject` (with `ClassDef`) contains a method? Or maybe that just returns the class version?

The use-case would be to be able to change switch between class versions at compile time, without changing user code, e.g.:

```cpp
MyObject *obj = new MyObject();
#if R__HasAttr(MyObject, SomeMethod) // or R__ClassVersion(MyObject) > 3
obj->SomeMethod();
#else
cout << "SomeMethod not yet implemented" << endl;
#endif

```

If I understand correctly, there is a way to do this at run time via `TObject::Class_Version()` and using a `TMethodCall`, so that, when switching to an old version, the compiler doesn’t complain that the method does not exist. But it would probably be nicer to have this check at compile time.

Thanks!

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [November 17, 2022, 4:26pm UTC](https://root-forum.cern.ch/t/preprocessor-macro-to-get-version-of-custom-class/52418/2 "2022-11-17T16:26:16Z")

</div>

I don’t think so, but maybe @pcanal or @Axel knows better than me…

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 18, 2022, 9:54am UTC](https://root-forum.cern.ch/t/preprocessor-macro-to-get-version-of-custom-class/52418/3 "2022-11-18T09:54:18Z")

</div>

That’s currently not possible.

The closest we could get to is making `MyObject::Class_Version()` `constexpr` (currently it’s only `static`) in which case you could use `if constexpr` (since C++17).

---

<div class="post-metadata">

**Author:** ![renyhp](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/e68b1a/32.png) [@renyhp](https://root-forum.cern.ch/u/renyhp)\
**Post date:** [November 18, 2022, 10:33am UTC](https://root-forum.cern.ch/t/preprocessor-macro-to-get-version-of-custom-class/52418/4 "2022-11-18T10:33:48Z")

</div>

Thanks! That would be a sensible thing to do, IMHO.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [November 18, 2022, 11:49am UTC](https://root-forum.cern.ch/t/preprocessor-macro-to-get-version-of-custom-class/52418/5 "2022-11-18T11:49:40Z")

</div>

> <https://github.com/root-project/root/pull/11740>
>
> allows to switch behavior based on the class version with \`if constexpr\`.
