60 pointsby mistiviaa day ago6 comments
  • srean16 hours ago
    In my grad school days, a couple of decades ago, I had written a library for my own use to facilitate chaining of different kinds of numeric operations on data vectors and sequences. Essentially, for a very simple form of deforestation [0,1], equipped with intermediate buffers to facilitate SIMD.

    GCC, surprisingly, was quite good at generating SIMD code from it and eliminating temporary data vectors. GCC was even quite good at explaining why it didn't emit SIMD that I wanted it to emit. Much to my surprise GCC was better in this regard than Clang. From what I had read about Clang at that time, it should have been the otherway round. The error messages were better too (wonders of competition).

    I quite liked it. It was a loads of fun. I would however be wary of using it in anger.

    The problem is, this sublanguage feels more like a dynamically typed language where errors would be thrown deep in the instantiation chain when it ultimately failed to instantiate.

    There was no type-system to guide you ahead-of-time that what you are trying to do would eventually fail to instantiate.

    The code got lost when Bitbucket stopped it's support for Mercurial. I still likely have it somewhere in my old files.

    Later I wanted to rewrite this in D but never started (thrown out of my university because I graduated).

    [0] https://en.wikipedia.org/wiki/Deforestation_(computer_scienc...

    [1] https://en.wikipedia.org/wiki/Expression_templates

    • ahartmetz15 hours ago
      It is pretty well known that C++ templates really are dynamically typed, it's compile time duck typing.

      What you did sounds like Eigen, which probably takes expression templates much further than you did due to the long time it's been around. It is weirdly absent from its documentation, but Eigen does have explicit SIMD support for several architectures and SIMD instruction sets. https://libeigen.gitlab.io/

      • srean15 hours ago
        Yes am familiar with Eigen. And yes I am/was that lunatic you mention in your other comment.

        This thing I wrote, predated Eigen (by a few years, I think) had a better support for pipelines and dataflow oriented programming (at least in the context of what I needed) than Eigen. It was more like itertools, functools of Python. Likely older than Eigen but positively after Blitz++ because I remember studying Blitz++ code.

        Unlike Eigen, mine had no explicit support for intrinsics, for that I relied on a smart enough compiler, GCC was surprisingly good.

        Too bad MSVC was quite bad at optimising and inlining away to the expression trees that Eigen (and mine) generated.

  • djmips13 hours ago
    This shows you how fun, useful and fascinating C++ template meta programming can be but also the dark side as a perfect example of inscrutable code that can be a real pain when you run across this sort of thing in production code with no documentation.
  • uecker15 hours ago
    This is very cool. I had a lot of fun doing C++ template meta programming two decades ago and the language got a lot more interesting. Just realize that you can waste a huge amount of time without anything doing remotely useful.

    (And please don't use any of it in any professional context.)

  • pjmlp19 hours ago
    While cool for those stuck in C++17, which are plenty given the C++ version of "can I use", C++26 should make many of these approaches more easily available.

    However it is still a few years away to be widely deployed, and a few niceties have been postponed into C++29.

  • ahartmetza day ago
    That could be pretty cool, I wonder what it does for compile time though.
  • jjmarr20 hours ago
    if this is a C++17 library why couldn't you use `constexpr` evaluation and not murder your compilation time?
    • mistivia19 hours ago
      Template metaprogramming isn't really suited for this task, the prime sieve here serves only as a proof of concept, meant to show the capabilities of this style. But there are cases where `constexpr` is not applicable, especially when involving type manipulations.

      On the other hand, C++ template metaprogramming, as an esolang, is fun to tame and experiment with.

      • jjmarr19 hours ago
        Is there a clearer example where constexpr wouldn't work?

        > On the other hand, C++ template metaprogramming, as an esolang, is fun to tame and experiment with.

        Is it an esolang at this point? I feel old.

        • ahartmetz16 hours ago
          Always has been (I'm over 40). It used to be a nightmare of a programming language, now with all the improvements over time it's merely quite bad. Its power to weight ratio is below any reasonable standard, but sometimes you need the power. Some lunatics like to play with it.
    • pjmlp19 hours ago
      I imagine because some of the cool constexpr improvements are only available in C++20 and C++23.