Информация об изменениях

Сообщение Re: Дурацкий вопрос от 23.11.2024 13:24

Изменено 23.11.2024 13:25 Pavel Dvorkin

Re: Дурацкий вопрос
Здравствуйте, kov_serg, Вы писали:

_>Почему 2 и 3 строчки не константы ?

_>
_>const int c1=(char*)1-(char*)0;
_>enum { c2=(char*)1-(char*)0 };
_>constexpr int c3=(char*)1-(char*)0;
_>


Насколько я понимаю, тут фактически reinterpret_cast.


The following solution:

class C {
public:
static constexpr const void* noop = reinterpret_cast<const void*>(0x1);
};
Although, it compiles and works fine in GCC isn't valid C++ (e.g., it doesn't compile with either clang or VC++2013) because according to the standard § 5.19/2 Constant Expressions [expr.const] the result of a reinterpret_cast can't be a constant expression.


https://stackoverflow.com/questions/24397797/statically-casting-an-integer-to-pointer-type
Re: Дурацкий вопрос
Здравствуйте, kov_serg, Вы писали:

_>constexpr int c3=(char*)1-(char*)0;


Насколько я понимаю, тут фактически reinterpret_cast.


The following solution:

class C {
public:
static constexpr const void* noop = reinterpret_cast<const void*>(0x1);
};
Although, it compiles and works fine in GCC isn't valid C++ (e.g., it doesn't compile with either clang or VC++2013) because according to the standard § 5.19/2 Constant Expressions [expr.const] the result of a reinterpret_cast can't be a constant expression.


https://stackoverflow.com/questions/24397797/statically-casting-an-integer-to-pointer-type