News for hackers. Stuff that matters.
Re:C++ by Example by kazinator on Thursday September 22, 2022 @01:51PM (#32942334)
Speaking of inheritance, how do you feel about this not requiring any diagnostics. It produces a weird result without containing any cast notation to subvert the type system.

  $ g++ -Wall -W -Wextra deriv.cc -o deriv
  $ ./deriv
  1.08654e-10

  $ cat deriv.cc
  #include <cstdio>

  struct Base {
  public:
    double y;
    Base() : y(1.0) {}
  };

  struct Derived : public Base  {
  public:
    int a, b;
    Derived() : a(0x3DDDDDDD), b(0x3DDDDDDD) {}
  };

  int main()  
  {
    Base *b = new Derived[5];
    b++;
    std::printf("%g\n", b->y);
  }
Re:C++ by Example by gpderetta on Friday September 23, 2022 @04:28AM (#32949342)
This is bad, but it is really no different than:

    int * b = new int;
    *++b;
Dereferencing past the end is always UB in C and C++ and it is hard to fix at the language level when using raw pointers.
Re:C++ by Example by jahnu on Thursday September 22, 2022 @02:16PM (#32942633)
For me this is the least of c++’s problems. Today I wouldn’t let that past a review as it should be using a std::array. If a c++ dev has a reason to drop down to this unsafe code they should know what the consequences are.