static-cast

static_cast / float / bitset / const weirdness

依然范特西╮ 提交于 2019-12-08 01:57:18
问题 Just a few hours ago, the following question came up: Variable cannot appear in a constant-expression Luckily for the OP, the answer provided did solve his problem, but I cannot reproduce the solution. I've attempted to simplify the code even more and I am now stuck with the following: #include <bitset> int main () { const size_t length_1 = static_cast<const size_t>(1.0f); std::bitset<length_1> bits_1; const size_t length_2 = static_cast<const size_t>(1.0f / 1.0f); std::bitset<length_2> bits

static_cast vs. direct call to conversion operator?

∥☆過路亽.° 提交于 2019-12-07 10:27:05
问题 Consider the following class, just as a simple example: #include <iostream> #include <string> using namespace std; class point { public: int _x{ 0 }; int _y{ 0 }; point() {} point(int x, int y) : _x{ x }, _y{ y } {} operator string() const { return '[' + to_string(_x) + ',' + to_string(_y) + ']'; } friend ostream& operator<<(ostream& os, const point& p) { // Which one? Why? os << static_cast<string>(p); // Option 1 os << p.operator string(); // Option 2 return os; } }; Should one call a

Why is a cast operator to std::optional ignored?

自闭症网瘾萝莉.ら 提交于 2019-12-07 05:49:10
问题 This code #include <iostream> #include <optional> struct foo { explicit operator std::optional<int>() { return std::optional<int>( 1 ); } explicit operator int() { return 2; } }; int main() { foo my_foo; std::optional<int> my_opt( my_foo ); std::cout << "constructor: " << my_opt.value() << std::endl; my_opt = static_cast<std::optional<int>>(my_foo); std::cout << "static_cast: " << my_opt.value() << std::endl; } produces the following output constructor: 2 static_cast: 2 in Clang 4.0.0 and in

Why do I need a reinterpret_cast to convert Fred ** const to void ** const?

拜拜、爱过 提交于 2019-12-06 20:20:34
问题 I have a const pointer to a pointer to a Fred and I don't understand why a static_cast isn't sufficient. typedef struct { int n; } Fred; Fred *pFred; Fred **const ppFred = &pFred; void **const ppVoid = static_cast<void ** const>(ppFred); Please could someone explain why a reinterpret_cast is needed to convert a pointer to Fred* to a pointer to void* but static_cast is fine to convert pointer to Fred to a pointer to void . 回答1: There's no requirement that a Fred* and a void* have the same size

static_cast / float / bitset / const weirdness

你离开我真会死。 提交于 2019-12-06 10:57:40
Just a few hours ago, the following question came up: Variable cannot appear in a constant-expression Luckily for the OP, the answer provided did solve his problem, but I cannot reproduce the solution. I've attempted to simplify the code even more and I am now stuck with the following: #include <bitset> int main () { const size_t length_1 = static_cast<const size_t>(1.0f); std::bitset<length_1> bits_1; const size_t length_2 = static_cast<const size_t>(1.0f / 1.0f); std::bitset<length_2> bits_2; } If compiled with -pedantic , the first example is accepted by the compiler, but the one with a

static_cast and temporary creation (final edition)

余生长醉 提交于 2019-12-05 16:54:57
Prerequisities: To understand this question, please, read the following question and its answer at first: Cast auto_ptr<Base> to auto_ptr<Derived> At Cast auto_ptr<Base> to auto_ptr<Derived> Steve answered that "Your static_cast would copy the auto_ptr to a temporary, and so aS would be reset and the resource would be destroyed when the temporary is (at the end of the statement)." I'm interested in the process of temporary creation while static_cast is called. I would like to have the code that I can trace in order to see this effect. I cannot use static_cast<auto_ptr<Circle>> ... because it

static_cast vs. direct call to conversion operator?

ぐ巨炮叔叔 提交于 2019-12-05 12:55:29
Consider the following class, just as a simple example: #include <iostream> #include <string> using namespace std; class point { public: int _x{ 0 }; int _y{ 0 }; point() {} point(int x, int y) : _x{ x }, _y{ y } {} operator string() const { return '[' + to_string(_x) + ',' + to_string(_y) + ']'; } friend ostream& operator<<(ostream& os, const point& p) { // Which one? Why? os << static_cast<string>(p); // Option 1 os << p.operator string(); // Option 2 return os; } }; Should one call a conversion operator directly, or rather just call static_cast and let that do the job? Those two lines will

Why is static_cast on an expression acting distributively?

荒凉一梦 提交于 2019-12-05 05:47:32
I need to take 2 unsigned 8-bit values and subtract them, then add this value to a 32-bit accumulator. The 8-bit subtraction may underflow, and that's ok (unsigned int underflow is defined behavior, so no problems there). I would expect that static_cast<uint32_t>(foo - bar) should do what I want (where foo and bar are both uint8_t ). But it would appear that this casts them first and then performs a 32-bit subtraction, whereas I need it to underflow as an 8-bit variable. I know I could just mod 256, but I'm trying to figure out why it works this way. Example here: https://ideone.com/TwOmTO

How to implement a compile-time check that a downcast is valid in a CRTP?

时光怂恿深爱的人放手 提交于 2019-12-05 04:40:05
I have a plain old CRPT (please don't get distracted by access restrictions - the question is not about them): template<class Derived> class Base { void MethodToOverride() { // generic stuff here } void ProblematicMethod() { static_cast<Derived*>(this)->MethodToOverride(); } }; that is as usual intended to be used like this: class ConcreteDerived : public Base<ConcreteDerived> { void MethodToOverride() { //custom stuff here, then maybe Base::MethodToOverride(); } }; Now that static_cast bothers me. I need a downcast (not an upcast), so I have to use an explicit cast. In all reasonable cases

Why do I need a reinterpret_cast to convert Fred ** const to void ** const?

点点圈 提交于 2019-12-05 00:34:49
I have a const pointer to a pointer to a Fred and I don't understand why a static_cast isn't sufficient. typedef struct { int n; } Fred; Fred *pFred; Fred **const ppFred = &pFred; void **const ppVoid = static_cast<void ** const>(ppFred); Please could someone explain why a reinterpret_cast is needed to convert a pointer to Fred* to a pointer to void* but static_cast is fine to convert pointer to Fred to a pointer to void . There's no requirement that a Fred* and a void* have the same size and representation. (I've worked on machines where they didn't, although that was before my C++ days.) When