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

Determine if objects stored in an std::vector are trivially copyable

This code doesn’t compile, since the static assert fails.

#include <vector>
#include <type_traits>

class A {
};

int main()
{
    std::vector<A> v;
    static_assert(std::is_trivially_copyable<decltype(v[0])>::value);
    return 0;
}

If, on the other hand, I replace the decltype(v[0]) with A, then it compiles. The problem is that v[0] is of type reference to A rather than A. How can I make it work using v in some way?

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

>Solution :

std::vector has a value_type member that is the type given in the parameter list of the vector for the element type. Using that gives you

static_assert(std::is_trivially_copyable<decltype(v)::value_type>::value);

Alternatively you can remove the reference qualification from v[0] like

static_assert(std::is_trivially_copyable<std::remove_reference<decltype(v[0])>::type>::value);
// or if you want to remove all cv, ref, and array qualification
static_assert(std::is_trivially_copyable<std::decay<decltype(v[0])>::type>::value);

Do note that if you are using a newer version of C++ like C++17 or C++20 then you can use the _t and _v versions of the type traits to simplify the code to

static_assert(std::is_trivially_copyable_v<decltype(v)::value_type>);

static_assert(std::is_trivially_copyable_v<std::remove_reference_t<decltype(v[0])>>);

static_assert(std::is_trivially_copyable_v<std::decay_t<decltype(v[0])>>);
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