cppinsights icon indicating copy to clipboard operation
cppinsights copied to clipboard

[feature] Support transforming C-style casts

Open Ukilele opened this issue 1 year ago • 2 comments

Hey Andres, a (probably not so) small feature request 🙂

The C-style cast is a pretty complex thing in C++: https://en.cppreference.com/w/cpp/language/explicit_cast . It follows a bunch of logical steps to find out what to actually do in the end. It would be convenient if cppsinsights would show which steps and which casts the compiler eventually executes.

Given following code:

struct Base {};
struct Derived : private Base {};

Derived d;
Base& b = (Base&)d;

const Derived d2;
Base& b2 = (Base&)d2;

It would be nice if cppinsights would show something like this:

struct Base
{
  // inline constexpr Base() noexcept = default;
};

struct Derived : private Base
{
  // inline constexpr Derived() noexcept = default;
};


Derived d;
Base & b = /*extended_*/static_cast<Base&>(d);

const Derived d2;
Base & b2 = const_cast<Base&>(/*extended_*/static_cast<const Base&>(d2));

It seems that this feature is already partly implemented. In the following code, the C-style cast gets correctly transformed into a reinterpret_cast: Code:

struct A { };
struct B { };

int main()
{
    A a;
    ((B*)&a);
}

Tranformation:

struct A
{
  // inline constexpr A() noexcept = default;
};

struct B
{
};


int main()
{
  A a;
  (reinterpret_cast<B *>(&a));
  return 0;
}

Maybe the feature is already partly implemented but only a few bits are missing?

Thanks and best regards, Kilian

Ukilele avatar Nov 28 '24 10:11 Ukilele