c++ – Cast Operators in C ++

Question:

I am reading "Discovering Modern C ++" now, and there is one place where the author writes that in C ++ there are four casting operators (everyone ..._cast ) and the (type)expr operator inherited from C.

In this regard, another theoretical question: the author forgot about casting type(expr) or is this casting, generally speaking, not a casting, in fact, being the creation of an object of type type from expr ? well, or by using a type conversion operator? After all, like here it said that this is also the type of conversion?

And yet – is there even a difference between conversion and casting, or are they synonyms?

And more about linguistics 🙂 – it clearly distinguishes between casting Up (from derivative to basic) and casting Down (of course, on the contrary) … But have I ever met such terms in our literature before?

Answer:

In this regard, another theoretical question: the author forgot about casting type (expr)

This can be considered a cast (as Myers and others put it as a cast), but the standard does not include it in the list of cast operators. Those. formally, this syntax is not a cast syntax. It has its own name in the standard: functional notation . In essence, this is the creation of an object initialized by expr . This is a call to the constructor.

And yet – is there even a difference between conversion and casting, or are they synonyms?

The first is a noun, the second is a verb (more often). No, there is no difference in terms of language.

But have I never met such terms before in our literature?

I have not read our literature for a long time, but if it is a translation of a book that includes up-casting and down-casting, it cannot but be translated.


From the syntax standard from the question: [expr.type.conv] p2

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) <…>

Where the corresponding cast expression is (T)expression

Scroll to Top