by int_19h · 6y ago · 0 comments
template<size_t N = sizeof(void*)> struct a;
template<> struct a<4> {
enum { b };
};
template<> struct a<8> {
template<int> struct b {};
};
enum { c, d };
int main() {
a<>::b<c>d;
}
This is not ambiguous, but the meaning of the line inside main() depends on the compiler and the target - it can be either a declaration: a<>::b<c> d;
or an expression: a<>::b < c > d;
depending on which b gets selected. This in turn affects future references to d etc.But yeah, it looks like a C++ compiler has to instantiate templates in lock-step with parsing to make this work, but possibly there are some tricks that are applicable here. In any case, this looks like a hard problem.
Nevertheless I think the C# way of choosing generics over comparison without semantic processing is strange. The example above should run as intended (as a simple function call with two arguments) and not fail because of some spec rule.