Do you guys have any strategies for dealing with templates when they have more than one optional parameter?

E.g let's say we have a type C that takes three parameters

struct B(T) {}
struct C(string name, T, string desc) {}

And let's say I want T and desc to be optional and T should be of type B, i.e. I would want to be able to do is this:

auto x = C!("name", B!int)();
auto y = C!("name", B!int, "desc")();
auto z = C!("name", "desc")();

So C is a template that must have a name, and can optionally have a B, or a description, or both.

The immediate (maybe naive) way that comes to mind is:

struct C(string name, rest...)
if (rest.length <= 2)
if (rest has one elements then it can be either template B or a string) // how? if (rest has two elements then it must be template B followed by string) // how?
  static if (rest[0] is type B) {
    enum hasB = true;
    static if (rest.length > 1) {
      string desc = rest[1];
  } else {
    enum hasB = false;
    if (rest.length) {
      string desc = rest[1];
    } else {
      string desc = null;

  // Now I have access to a desc if it was provided
  // And a boolean that tells me if I have a B type.
  // But dayum that was a lot of hoops.

Giving it some more thought, one could do this too:

struct Void {}

struct C(string name, T, string desc)
if (isInstanceOf!(B, T) || is(T == Void)
template C(string name T) {
  alias C = C!(name, T, null);
template C(string name, string desc) {
  alias C = C!(name, Void, desc);

Which requires one to create this meta-Void type to get to work. Which I guess is not that bad. But, anyway.

Are there other ways? Or does anyone have any tips on how to deal with this? Or is the alias way generally the way to go?

- Ali

Reply via email to