I'm not sure how to explain this, so I'll give the code.
I'm trying to encode (for fun, also to help me understand template metaprogramming) Chuch numerals using C++ templates.
struct Z {
static constexpr unsigned int n = 0;
template<typename T>
using replace = T;
};
template<typename T>
struct S {
static constexpr unsigned int n = T::n + 1;
template<typename U>
// using replace = T::replace<U>;
using replace = typename T::template replace<U>;
};
So, I tried using replace = T::replace<U>
, which really didn't work, and I had no idea why (I was using GCC), and I'd appreciate if someone could explain why to me (see link below). When I tried compiling with Clang, it gave me a nice error message:
First, use 'template' keyword to treat 'replace' as a dependent template name
, then template argument for template type parameter must be a type; did you forget 'typename'?
...
Then I managed to make using replace = typename T::template replace<U>
compile, this seems legal.
But what I actually needed was something like S<T::replace<U>>
, which I found no way to make work.
I tried changing this to T::replace<U>::succ
as well, and defining succ
to be S<Z>
on Z
and S<S<T>>
on S
, but I couldn't make it work either...
Any ideas? :(