D
D
Dmitry2020-08-01 19:45:42
C++ / C#
Dmitry, 2020-08-01 19:45:42

Boolean type bool and operator new. What's going on here?

Why does the compiler allow me to do this: Doesn't the operator return a pointer to the chosen type? Or is it some type feature ? Because for the record: - the compiler swears.
bool value1 = new bool(true);

new
bool

int value1 = new int(1);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AnT, 2020-08-01
@R3cive

In your question, you have a new-expression that returns a pointer of type bool *. Where did you suddenly get "a pointer to void" - it's not clear. New-expression never returns "pointer to void".
At the same time, any pointer in the C++ language is implicitly cast to the type bool. This is what is used in your example.
Additionally, you can notice that since C++20, the conversion of pointers to a type boolis narrowing (narrowing). This means that it, in particular, will no longer be implicitly executed in the context of list initialization . I don't really remember offhand whether this change was eventually accepted as a C++17 defect or as an incompatible language change.
bool value1{ new bool(true) }; // Ошибка

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question