June 16, 2003
Metacode injection and decltype

Until last Thursday, continuing the metacode implementation hadn't made it to the top of my todo list for a while. Fortunately, the folks at the Open Systems Lab (OSL) at Indiana University invited me for a visit to talk about metacode and other C++ language issues. Besides the opportunities for discussion, this also got me into airplanes, airports, and hotels, which is always the perfect venue for writing code.

As a result of this coding the metacode interpreter (embedded in the compiler) now supports a few more C++ constructs. I also added the is_constant metacode library primitive. More significantly, simple cases of metacode expression injection (the return-> construct) now work, so that the following example does the expected thing:

metacode 
int mypow(int b, int n) {
  using ::stdmeta::is_constant;
  if (is_constant(b) &&
      is_constant(n) &&
      n >= 0) {
    return power<>(b, (unsigned)n); // Compile-time
  } else {
    return-> ::std::pow((double)b, (double)n);
  }
}

(I wish I could write the example above with type double, but the interpreter doesn't handle floating-point types yet.)

While chatting about all these things, we also ended up rehashing some aspects of the decltype proposal (OSL's Jaakko Järvi is one of the principal movers of the decltype proposal). This proposal came out of discussions we had in the evolution working group (EWG) regarding an adaptation of the widely implemented typeof extension. While typeof is useful for some purposes, many generic library implementers expressed the need for an operator that expands to a reference type in some cases. (typeof expands to the type of a given expression, and expressions never have reference type.) For example, decltype(f()) should expand to the return type of the selected function f rather than to the type of the expression f().

Eventually we whipped up a couple of implementations of decltype (as an “exercise”). The facility is therefore available in my metacode-enabled front end.

Posted by Daveed at 09:43 AM