1

Nested templates require the usage of template keyword, as explained in this answer. Here's a concise demonstration that results in a compiler error:

template <int T>
struct A {
  template <int U>
  static void b(int x) {
  }
};

template <int T>
void test() {
  A<T>::b<T>(T); // Should be:
  // A<T>::template b<T>(T);
}

int main() {
  test<0>();
}

As a challenge, I was trying to think of a program where two possibilities (with or without template) are both valid but have two different meanings, but I couldn't wrap my head around the operator overloading business. Is it possible to make A<T>::b<T>(T); a valid statement?

1 Answers1

-1

If you change your struct A for:

template <int T>
struct A {
    static const int b = 5;
};

Then A<T>::b<T>(T); would compile.

  • In that case, A<T>::b would be 5.
  • T is 0.
  • Thus 5 < 0 is false (0)
  • 0 > (0) is false too;

If you replace the code with something like:

auto x = A<T>::b<T>(T);
std::cout << typeid(x).name() << " " << x;

You will get:

bool 0

Thus it was not too hard to modify A so that A<T>::b<T>(T) is a valid statement.

O'Neil
  • 3,790
  • 4
  • 16
  • 30
Phil1970
  • 2,605
  • 2
  • 14
  • 15