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?
std::vectorhas avalue_typemember that is the type given in the parameter list of the vector for the element type. Using that gives youAlternatively you can remove the reference qualification from
v[0]likeDo note that if you are using a newer version of C++ like C++17 or C++20 then you can use the
_tand_vversions of the type traits to simplify the code to