4

I've seen these, what appear to be functions in c++ code but I have no idea what they do or what they are, they appear to do similar things to typecasts, but they don't look like typecasts, so what are they?

They look like this: int(x) where x is some numeric input, I've been looking around online but I haven't been able to find any information about them (probably because I don't know what they're called).

Any help?

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131

3 Answers3

5

This is called the "functional notation", and it is a form of explicit type conversion ("casting"). C++11, 5.2.3:

5.2.3 Explicit type conversion (functional notation)

  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.
Community
  • 1
  • 1
4

int(x) is a functional notation for type-casting.

C++ is a strong-typed language. Many conversions, specially those that imply a different interpretation of the value, require an explicit conversion, known in C++ as type-casting. There exist two main syntaxes for generic type-casting: functional and c-like:

The functionality of these generic forms of type-casting is enough for most needs with fundamental data types.

Example:

double x = 10.3;
int y;
y = int (x);    // functional notation
y = (int) x;    // c-like cast notation 

Check out what is the difference between c-like casting & functional casting? for more info.

Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
3

This is an alternative syntax of the type cast operator to make it look like a copy constructor syntax and thus be syntactically appropriate in the places where copy constructor does. int(x) and (int)x have the same effect.

bobah
  • 18,364
  • 2
  • 37
  • 70
  • 1
    No, these are not constructors. [Primitive types have no constructors](http://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors). These are called functional-style casts. –  Jan 25 '14 at 09:48
  • 1
    @H2CO3 - true, but they behave the same way and in most cases are indistinguishable from copy constructors functionally. This is why I quoted "copy constructors" in my answer. – bobah Jan 25 '14 at 09:50
  • true, but they **are not** constructors. When you drop an egg and a glass on the floor, both break into pieces, so they behave in the same way. Yet an egg is not a glass. –  Jan 25 '14 at 09:51
  • @H2CO3 - read my answer again, slowly, and then think again whether your critics really applicable – bobah Jan 25 '14 at 09:55
  • I have read it carefully for the first time. And having re-read it haven't changed the fact that you state something incorrect in the first part of your answer. –  Jan 25 '14 at 09:56
  • Did you read the third sentence too or stopped after the first two? – bobah Jan 25 '14 at 10:01
  • 3
    Regardless of whether you want to say that `int` in effect has constructors, "copy constructors" do not explain what's happening. A copy constructor for `int` would take `int&` or `const int&`. Anything else would not be a copy constructor. However, `int` can be converted from other types. – Steve Jessop Jan 25 '14 at 10:07
  • I edited the answer to even simpler form. I hope it won't cause more ambiguity. – bobah Jan 25 '14 at 10:13