What is an int() Called?

后端 未结 3 857
闹比i
闹比i 2020-12-10 12:14

It\'s been rehashed over and over that primitive types don\'t have constructors. For example this _bar is not initialized to 0 when I call Foo():

3条回答
  •  生来不讨喜
    2020-12-10 12:32

    Here's what int() does (bearing in mind that, grammatically, int is a simple-type-specifier):

    [C++11: 5.2.3/1]: A simple-type-specifier (7.1.6.2) or typename-specifier (14.6) followed by a parenthesized expression-list constructs a value of the specified type given the expression list. If the expression list is a single expression, the type conversion expression is equivalent (in definedness, and if defined in meaning) to the corresponding cast expression (5.4). If the type specified is a class type, the class type shall be complete. If the expression list specifies more than a single value, the type shall be a class with a suitably declared constructor (8.5, 12.1), and the expression T(x1, x2, ...) is equivalent in effect to the declaration T t(x1, x2, ...); for some invented temporary variable t, with the result being the value of t as a prvalue.

    Speaking colloquially, it represents the construction of a temporary int with an empty initialiser. I think you'd struggle to find a formal name for the entire construct, though.

    This is not the same as int i{}, which is a full-fledged declaration of a named object with an initialiser: your i has been declared, constructed and initialised.

    (I don't think that any of this is related to what Loki was saying in his comment on that linked answer.)

提交回复
热议问题