Search code examples
templatesd

mixin templates: how to halt compilation?


I have a mixin template which is only valid with certain arguments. I want to halt the compilation with an error message if the arguments are invalid. For templates I'd use assert(false, "Invalid args for Yoo") but this does not work for mixin templates. How to halt compilation for the example below?

mixin template Yoo(args...) {
  static if (args.length == 0) {
    pragma(msg, "Invalid args! (how to halt the compilation?)");
  } else {
    pragma(msg, "Valid args:", args);
  }
}

void main() {
  mixin Yoo;
  mixin Yoo!(1,2,3);
}

Solution

  • you can do

    static assert(0, "Invalid args!");
    

    instead of the pragma msg. Static assert is like assert, just compile time and it will not get removed in release mode because its only checked while compiling and not included in the compiled code.