Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When using std::is_base_of to obtain value, why can {} or () replace ::value?

enter image description here
Why are these three outputs equivalent and what syntax is this?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Because std::is_base_of (and many other traits) "returns" std::true_type or std::false_type which are template instances of std::integer_constant<T, value>.

A variable instance of type std::integer_constant<T, value> (which is constructed with {}) is implicitly convertible to T and its compile-time value returned by the conversion.

constexpr bool flag = std::is_base_of<B, D>{}; 

I used to have this notation std::is_base_of<Base, Derived>{} to evaluate it in conditions to save typing, but this can be confusing, and there is no reason for this since one can use std::is_base_of_v<B, D> nowadays.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading