473,408 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

VC6 recursive template enum definition

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. However, I
have no luck that I must use VC6. Could anyone offer me a workaround?
Thanks in advance.

Jul 23 '05 #1
2 2037
Amon Tse wrote:
I have the following code which is unable to compile in VC6:
[...]
This likely could be solved if I had switched to use VC7.1. However, I
have no luck that I must use VC6. Could anyone offer me a workaround?


Please ask compiler-specific questions in the newsgroups dedicated to
those compilers (microsoft.public.vc.language in this case).
Jul 23 '05 #2
"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

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
7
by: Senthilvel | last post by:
Hi folks, I was trying to learn the standard library algorithms where i got stuck with the templates. I wrote one function to eliminate the duplicates(from The C++ Programming Language 3rd...
7
by: Thomas Matthews | last post by:
Hi, I am converting my table and record classes into templates. My issue is the syntax of declaring a friend class within the template. I have searched the C++ FAQ Lite (web), the C++...
1
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive...
1
by: mathieu | last post by:
Hello there, I am playing around with template metaprograming: I am trying to redefines my own types. But I am facing a small issue, where I cannot describe the whole implementation in one...
5
by: Mark Stijnman | last post by:
I am trying to teach myself template metaprogramming and I have been trying to create lists of related types. I am however stuck when I want to make a template that gives me the last type in a...
16
by: Hendrik Schober | last post by:
Hi, suppose we have template< typename T > struct X; and some specializations: template<>
7
by: er | last post by:
hi, could someone please help with this code? template<unsigned int N,unsigned int M> class A{ public: A(); };
4
by: andreyvul | last post by:
I have a template function like this: std::vector<std::pair<Square<T>, Triangle<T * recarrow(T a, T b, int n) { /* ... */ } template <class T> struct Square { /* ... */ };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.