"Amon Tse" <am*****@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com
I have the following code which is unable to compile in VC6:
#include <iostream>
template<size_t target, size_t idx = 0>
struct _Iterator
{
enum
{
value = (target == idx) ? idx : _Iterator<target, idx+1>::value
};
};
int main()
{
std::cout << _Iterator<3>::value << std::endl;
// the expected result is 3
return 0;
}
The compilation error is
fatal error C1202: recursive type or function dependency context too
complex
This likely could be solved if I had switched to use VC7.1.
Actually no. You have an infinite loop. The problem is that in the line
value = (target == idx) ? idx : _Iterator<target, idx+1>::value
_Iterator<target, idx+1>::value is evaluated regardless of whether or not
target == idx.
However, I
have no luck that I must use VC6. Could anyone offer me a workaround?
If you were using VC++7.1, you could do it this way:
#include <iostream>
template<size_t target, size_t idx = 0>
struct _Iterator
{
enum
{
value = _Iterator<target, idx+1>::value
};
};
template <size_t target>
struct _Iterator<target, target>
{
enum
{
value = target
};
};
This won't work for VC++6.0 since it doesn't support partial template
specialisation.
As for finding a workaround, it is not clear to me what your code is
attempting to do. Something far simpler like
template<size_t target>
struct _Iterator
{
enum
{
value = target
};
};
will produce an output of 3 from your code in main(). What more do you want?
--
John Carson