August 19, 2023

I've just read this article about C++23 user defined typequals.

Thanks to alias this D can do the same since a long time and without new language feature:

import std;

enum Qual;

template Qualified(Q,T)
{
    @Q struct Qualified
    {
        T t;
        alias t this;
    }
}

struct S
{}

alias SQual = Qualified!(Qual,S);

void filterQual(T : S)(T t)
if (hasUDA!(T, Qual))
{}

void both(T : S)(T t)
{}

void main()
{
    S s;
    SQual sq;

    both(sq);
    both(s);
    filterQual(sq); // OK
    filterQual(s);  // NG
}

Not sure how (and if) the C++ version will gain more usability, however.
As note the author

>

A more interesting challenge is the following: As laid out, this technique implements syntactic qualifier subtyping, but does not do anything towards enforcing the semantics associated to each qualifier

August 22, 2023

On Saturday, 19 August 2023 at 19:56:54 UTC, Basile B. wrote:

>

As note the author

>

A more interesting challenge is the following: As laid out, this technique implements syntactic qualifier subtyping, but does not do anything towards enforcing the semantics associated to each qualifier

The only way to enforce semantics for C++ and D would be via creating a compiler plugin that enforces the semantics, I think.

The compiler can't perform the necessary semantics analysis if the logic for that is not present in it. The languages aren't even interpreted, such that the code to modify the behavior towards certain types/qualifiers could be presented on-the-fly and the interpreter/VM could adapt dynamically.

One can write C++ plugins, atleast for clang, using https://clang.llvm.org/docs/Tooling.html#clang-plugins

For D plugins, atleast for LDC, we most likely now have https://forum.dlang.org/post/clelngkqvihzybpxgldq@forum.dlang.org