Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the term "trivial" formally mean in C++?

I found the term in this chain of concepts:

  • (1) trivially copyable constructor

  • (2) trivial defaut constructor (defined from (1) )

  • (3) trivialType (defined from(2))
    Techinically, I understand all the concepts (like what exactly it contains)
    Semantically, I don't understand the term "trivial" (explain why I keep forgeting the chain).
    I try to look up the word in as many dictionaries as possible, but no sense of it make sense to me, and here is my work (if any one care):
    --------------------
    *trivial:

  1. of little value or importance.
    [OXF] not important or serious; not worth considering
    =>a trivial detail
    =>I know it sounds trivial, but I'm worried about it.
    =>huge fines were imposed for trivial offenses
    ---------
    1X. A trivial problem = is easy to solve: Getting computers to understand human language is not a trivial problem.
    ----------
  2. relating to or being the mathematically simplest case: specifically, characterized by having "all variables" equal to "zero"
    [DIC] noting "a solution of an equation" in which "the value of every variable" of "the equation" is equal to zero.
    =>a trivial solution to a linear equation
    ----------
    2X. (of a theorem, proof, or the like) simple, transparent, or immediately evident.
    =>NONE
    ---------
  3. (of names of organisms) specific, as distinguished from generic.
    [MER] SPECIFIC sense 4
    =>NONE
    --------
  4. commonplace; ordinary.
    [MER] COMMONPLACE, ORDINARY
    =>NONE
    ---------------------
    I read all the concepts in the chain, and seem to understand them all. And then a few days later, I see one of that terms again and it seems like a stranger to me again. So I have to read the whole chain again. And a few days later it happens again. And I find myself in an infinate loop.
    So what the term "trivial" semantically or literally mean in C++ and why the term is chosen, please help.
like image 957
Crackie Avatar asked Oct 29 '25 08:10

Crackie


1 Answers

Terms such as trivial are defined in the C++ standard, and you can look up their definitions. Those are most easily found in the index of the latest working draft.

Intuitively, a trivial type is a type where all operations (default construction, copying, destruction, ...) are equivalent to doing nothing, or performing std::memmove from one object to another (ignoring padding). int is a trivial type for example, since construction and destruction of an int requires no action, and copying it is just copying its bytes (as if by std::memmove).

Formally, [basic.types.general] p9 defines what a trivial type is. In short, it's a scalar type (e.g. int, void*, etc.), an array of trivial types, or a trivial class.

A trivial class is a class that is trivially copyable and has one or more eligible default constructors, all of which are trivial.

- [class.prop] p2

A default constructor is trivial if it is not user-provided and if:

  • [...]

- [class.default.ctor] p3

In short, it means that a default constructor "has to do nothing" to be trivial. From this and other definitions, you can figure out exactly what the C++ standard means by trivial.

like image 83
Jan Schultke Avatar answered Oct 31 '25 23:10

Jan Schultke