Hacker News new | ask | show | jobs
by acehreli 1946 days ago
Yes, C++ has been catching up since C++11.

One thing C++ will never have is D's `static if`, which allows injecting declarations into scopes. Bjarne Stroustrup and others closed that door: https://isocpp.org/files/papers/n3613.pdf

2 comments

> One thing C++ will never have is D's `static if`, which allows injecting declarations into scope

don't be so sure

https://github.com/lock3/meta/wiki/Metaprogramming-Introduct...

a lot of it is already implemented: https://cppx.godbolt.org/z/4arx6T

Thanks for letting me know. This seems to be far from being adopted into C++, if at all, right? Is the C++ community behind this effort?
It has been proposed by Herb Sutter a few years ago and has been worked on since by Andrew Dutton, so yes the c++ community is definitely behind it.

See e.g. https://youtu.be/8c6BAQcYF_E

Seems horrifically complicated?
it allows to generate code at basically every scope and to create your own "base language elements" like "struct", "class". AFAIK D does not allow for this, does it ?
D's templates has always been very strong and useful compared to C++'s.

The following example is similar to C macros because it generates code as string and mixes it in as code but you can combine templates as well. This example generates a struct definition.

(Edit: formatting)

  import std; // Entire package for brevity
  
  auto memberDef(string type, string name) {
    return format!"  %s %s;"(type, name);
  }
  
  auto memberDefs(string[] members...) {
    return (members
            .chunks(2)
            .map!(pair => memberDef(pair[0], pair[1])));
  }
  
  auto structDef(string name, string[] members...) {
    return format!q{
      struct %s {
        %-(%s%)
      }
    }(name, memberDefs(members));
  }
  
  unittest {
    mixin (structDef("Foo", "int", "i", "double", "d"));
    auto f = Foo(42, 1.5);
    assert((f.i == 42) && (f.d == 1.5));
  }
  
  void main() {
  }
Sounds like something `if constexpr ()` comes close to