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

c++ standard_layout for ref types

I’m trying to understand why standard_layout does not apply to ref types?

#include <type_traits>

struct X {
    int y;
};

static_assert(std::is_standard_layout_v<X>);
static_assert(std::is_standard_layout_v<X&>); // does not compile

>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

As you’ve found, is_standard_layout only works with an object type, not a reference.

As such, if you want either a type or a reference to a type, you could use:

static_assert(std::is_standard_layout_v<std::remove_reference_t<X>>);

remove_reference_t will yield the referred-to type for a reference, or the type itself if what you pass isn’t a reference (note: for older compilers, you can use std::remove_reference<T>::type).

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