Which type trait would indicate that type is memcpy assignable? (tuple, pair)

后端 未结 3 1273
遥遥无期
遥遥无期 2021-02-08 09:57

I would like to know what type introspection I can do to detect types that assignable by simply raw memory copy?

For example, as far I understand, built-in types tuples

3条回答
  •  死守一世寂寞
    2021-02-08 10:35

    This is only a partial answer to your question:

    Type traits don't necessarily mean what their name says literally.

    Specifically, let's take std::is_trivially_copyable. You were - rightly - surprised that a tuple of two double's is not trivially copyable. How could that be?!

    Well, the trait definition says:

    If T is a TriviallyCopyable type, provides the member constant value equal true. For any other type, value is false.

    and the TriviallyCopyable concept has the following requirement in its definition:

    • Every copy constructor is trivial or deleted
    • Every move constructor is trivial or deleted
    • Every copy assignment operator is trivial or deleted
    • Every move assignment operator is trivial or deleted
    • At least one copy constructor, move constructor, copy assignment operator, or move assignment operator is non-deleted
    • Trivial non-deleted destructor

    Not quite what you would expect, right?

    With all in mind, it's not necessarily the case that any of the standard library traits would combine to fit the exact requirements of "constructible by memcpy()'ing".

提交回复
热议问题