Answer the question
In order to leave comments, you need to log in
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
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 bool
is 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 questionAsk a Question
731 491 924 answers to any question