C++ question: where does int() come from ?

John (Eljay) Love-Jensen eljay@adobe.com
Wed Dec 20 12:28:00 GMT 2006


Hi Daniel,

> Actually it is not a "constructor cast", but a constructor call. In C++, even the built-in types (such as int, double, ...) have a default- and a copy-constructor.

> int(<some integer expression>) invokes the copy-constructor for the int type, resulting in a (temporary) int instance. The type conversion ("cast") is performed even before that:'A' is (by integer promotion) implicitly extended to an int while being passed to the copy-constructor.

Sounds good to me.  As a devout pedant, I appreciate the clarification.

The idiom of using int('A') was called a "constructor cast" by my C++ co-workers, so the (regional?) terminology for that pattern is what I was describing.  And that terminology goes back to at least 1995, to my earliest recollection of its usage.

A quick Google of "constructor cast" pulls up a few (~30) pages which mention that pattern.  So it appears that the terminology for the pattern isn't widespread throughout the C++ community.  Just a smattering.  As far back as 2001.

Hmm, in one spot, the term "coercion cast" is also used for that pattern.

Assuming one of the Google pages is correct, the standard calls it "explicit type conversion (functional notation)".

Apparently -- as you pointed out -- it is not (literally) a cast, so calling it a "constructor cast" is a bit of a misnomer along the lines of calling bald a hair color.

Caveat - a "constructor cast" is only good for making rvalues.

Sincerely,
--Eljay



More information about the Gcc-help mailing list