cppfront icon indicating copy to clipboard operation
cppfront copied to clipboard

[BUG] sidecasts via is/as always fail

Open gerlero opened this issue 3 years ago • 0 comments
trafficstars

Describe the bug When attempting to sidecast (in a scenario with multiple inheritance) using the is/as syntax, all cast attempts fail—even when an equivalent dynamic_cast would succeed.

To Reproduce Steps to reproduce the behavior:

  1. Sample code - distilled down to minimal essentials please
struct B1 { virtual ~B1() = default; };

struct B2 { virtual ~B2() = default; };

struct D : B1, B2 {};

main: () -> int = {
    d: D = ();
    p: *B1 = d&;

    std::cout << p* is B2 << std::endl;
}
  1. Command lines including which C++ compiler you are using x86-64 gcc 12.2 with -std=c++20 option

  2. Expected result - what you expected to happen Program compiles and outputs 1 (i.e., p* is a B2)

  3. Actual result/error Program compiles but prints 0

Additional context The cpp2::is and cpp2::as overloads that would actually try a dynamic_cast in this situation are never considered because they are currently constrained to downcasts only (with is_base_of checks in their requires clauses).

EDIT: fix typo in code snippet

gerlero avatar Nov 21 '22 07:11 gerlero