user1095108 user1095108 - 7 months ago 29
C++ Question

is a trivially copyable ::std::tuple-like class template possible? Does an implementation exist?

I need a trivially copyable tuple-like class, but no suitable implementations exist, I could not come up with one myself and I think one might even not be possible. The reason for it are references. A

can hold references, but a trivially copyable tuple might not be able to, since it may not have non-trivial constructors and references would have to be initialized in the constructors of the tuple-like class and storing a reference wrapper would make the tuple-like class non-trivial. My questions are in the title.


Storing references using reference_wrapper is entirely possible:

std::reference_wrapper is guaranteed to be TriviallyCopyable. (since C++17)

Just having a non-trivial non-special constructor (as e.g. std::reference_wrapper<T>::reference_wrapper(T&)) is absolutely fine. So the same holds for your trivially_copyable_tuple; as long as it has a trivial copy constructor, trivially_copyable_tuple::trivially_copyable_tuple(int&, float&, char) is fine.

And actually, you don't need to use std::reference_wrapper at all; while a reference type is not TriviallyCopyable, a class type containing a reference is itself TriviallyCopyable (although it is not Pod, StandardLayoutType, DefaultConstructible, TriviallyDefaultConstructible, or Trivial).

Here's a few examples: