static-cast Questions
4
Solved
Why i can't use reinterpret_cast operator for such a cast?
enum Foo { bar, baz };
void foo(Foo)
{
}
int main()
{
// foo(0); // error: invalid conversion from 'int' to 'Foo'
// foo(reinterpret_...
Ingather asked 1/9, 2012 at 14:26
3
Solved
I was trying to implement static polymorphism using the Curiously Recurring Template Pattern, when I noticed that static_cast<>, which usually checks at compile time if a type is actually con...
Lynnlynna asked 10/1, 2018 at 16:35
0
I am writing a C++ tagged pointer. I have a question about whether the operations I use to implement its basic functionality cause undefined behavior:
When constructing a tagged pointer given a po...
Poeticize asked 23/1, 2024 at 3:16
5
Solved
struct A{};
struct B : A{};
int main()
{
A a;
A& a_ref = a;
static_cast<B>(a); // *1
static_cast<B&>(a_ref); // *2
return 0;
}
(*1) produces an error and i understand ...
Domash asked 30/10, 2013 at 13:3
7
Is it OK to cast a double array to a struct made of doubles?
struct A
{
double x;
double y;
double z;
};
int main (int argc , char ** argv)
{
double arr[3] = {1.0,2.0,3.0};
A* a = static_cas...
Radarscope asked 26/6, 2015 at 21:26
1
Solved
Simple question: Will the conversion from an int, say 100, to a double "round" up or down to the next double or will it always round to the nearest one (smallest delta)?
e.g. for static_c...
Fiberboard asked 8/3, 2023 at 7:46
9
Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other?
Planogamete asked 21/11, 2008 at 22:39
1
Solved
We ran into this scenario in our codebase at my work, and we had a big debate over whether this is valid C++ or not. Here is the simplest code example I could come up with:
template <class T>...
Socman asked 30/7, 2022 at 1:5
1
I read somewhere that a pointer-to-member of a derived class can be converted to pointer-to-member of its base class. When I have read this, my smart mind started to think of many examples and keep...
Pulpboard asked 20/3, 2022 at 13:45
1
Solved
According to [expr.cast]/4, a C-style cast tries the following casts in order:
const_cast
static_cast
static_cast followed by const_cast
reinterpret_cast
reinterpret_cast followed by const_cast
...
Recriminate asked 30/3, 2018 at 21:47
2
Solved
I have a pointer T * pValues that I would like to view as a T (&values)[N]
In this SO answer https://stackoverflow.com/a/2634994/239916, the proposed way of doing this is
T (&values)[N] = *...
Osculate asked 23/1, 2014 at 19:3
0
ISO/IEC 14882:2020
22.2.1.16
Note 8:
The default construct in allocator will call ::new ((void*)p) T(args), but specialized allocators can choose a different definition.
Would you be so kind as t...
Pes asked 28/12, 2021 at 8:11
1
Solved
To my surprise, gcc 11.2 accepts this code, but only in C++20 mode:
struct Base {};
struct Derived : Base { int i; };
int f(Base& b) { return static_cast<Derived>(b).i; }
// ^~~~~~~ oops,...
Winepress asked 10/8, 2021 at 7:32
1
Solved
Given the following conditions:
struct A
{
int a;
};
struct B
{
int b;
};
int main()
{
A a {1};
A* p = &a;
Does casting with static_cast and with reinterpret_cast via void* give the same...
Counterblow asked 25/6, 2021 at 21:35
5
Solved
Given a program
#include <iostream>
using namespace std;
int main()
{
const size_t DoW = 7;
const unsigned int DAYS_OF_WEEK = static_cast<unsigned int> (DoW);
unsigned int dayOfFirs...
Adverse asked 17/5, 2021 at 10:32
4
Solved
The description for static cast says
If new_type is an rvalue reference type, static_cast converts the value of expression to xvalue. This type of static_cast is used to implement move semantics...
Aponte asked 17/6, 2013 at 7:14
1
Solved
Taken from the GCC implementation of type_traits why is static_cast needed here?
template <typename _Tp, typename... _Args>
struct __is_nt_constructible_impl
: public integral_constant<b...
Perrie asked 7/1, 2020 at 11:28
1
Solved
For the following code, all but the last assertion passes:
template<typename T>
constexpr void assert_static_cast_identity() {
using T_cast = decltype(static_cast<T>(std::declval<T...
Waldo asked 5/10, 2019 at 21:39
2
Solved
I've a question to the code snippet below:
long l=9223372036854775807L;
float f=static_cast<float>(l);
The long value cannot be represanted exactly according to the IEEE754.
My Question ...
Admiration asked 10/9, 2019 at 12:0
1
Solved
C++17 makes noexcept part of a function's type. It also allows implicit conversions from noexcept function pointers to potentially throwing function pointers.
void (*ptr_to_noexcept)() noexcept = ...
Beleaguer asked 21/8, 2019 at 17:4
3
Solved
I have a type A that's designed to be implicitly casted to type B. Here's an example case I'd like to use it in:
// Current implementation:
std::transform(vec_of_a.begin(), vec_of_a.end(), std::ba...
Antisepsis asked 13/9, 2016 at 22:20
5
Solved
My code
class Parent { int a; };
class Child { int b; };
struct GrandChild : public Parent, public Child { int a, b, c; };
int main() {
GrandChild GC;
std::cout << "GrandChild's address i...
Anklebone asked 8/8, 2015 at 8:10
1
Solved
The following code compiles without any error in VSC++2017 and doesn't compile in gcc 7.3.0 (error: invalid static_cast from type ‘int(int)’ to type ‘void*’
void* p = static_cast<void*>(func...
Christchurch asked 24/3, 2019 at 0:18
1
Solved
I have used the CRTP pattern for a while, however reading answers about undefined behaviour related to downcasting I don't understand why the static_cast<Derived&>(this), where this is of...
Mindi asked 19/2, 2019 at 3:1
2
My question is motivated by this answer on stackoverflow, https://mcmap.net/q/263739/-how-you-convert-a-std-string_view-to-a-const-char. To quote,
Q: How you convert a std::string_view to a const ...
Crocked asked 16/1, 2019 at 6:22
1 Next >
© 2022 - 2025 — McMap. All rights reserved.