Topic: recursive #include ques.
Author: "Paul Mensonides" <pmenso57@attbi.com>
Date: Thu, 4 Apr 2002 22:00:11 GMT Raw View
Is it illegal to have a file include itself if the recursion is bounded in some
way?
Paul Mensonides
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Paul Mensonides" <pmenso57@attbi.com>
Date: Thu, 4 Apr 2002 22:51:05 GMT Raw View
Is it illegal to have a file include itself if the recursion is bounded in some
way?
Paul Mensonides
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Fri, 5 Apr 2002 04:43:20 GMT Raw View
Paul Mensonides wrote:
>
> Is it illegal to have a file include itself if the recursion is bounded in some
> way?
I believe so. I didn't find any prohibition on it in the obvious place
(section 16.2), and I can't see any reason why it shouldn't be allowed.
However, remember that there's an implementation-defined nesting limit
on nested #includes, and a recursive #include also counts as a nested
#include. The recommended minimum value for the nesting limit is 256,
but a legal implementation could have a limit of 0, so long as it
documents the fact.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Paul Mensonides" <pmenso57@attbi.com>
Date: Fri, 5 Apr 2002 05:10:55 GMT Raw View
"James Kuyper Jr." <kuyper@wizard.net> wrote in message
news:3CAD18FF.4A96E@wizard.net...
> Paul Mensonides wrote:
> >
> > Is it illegal to have a file include itself if the recursion is bounded in
some
> > way?
>
> I believe so. I didn't find any prohibition on it in the obvious place
> (section 16.2), and I can't see any reason why it shouldn't be allowed.
> However, remember that there's an implementation-defined nesting limit
> on nested #includes, and a recursive #include also counts as a nested
> #include. The recommended minimum value for the nesting limit is 256,
> but a legal implementation could have a limit of 0, so long as it
> documents the fact.
I was just testing a file to expand a macro with a given series of numbers:
// [file.h]
#ifndef UPPER_BOUND
#error UPPER_BOUND is not set
#else
#ifndef LOWER_BOUND
#define LOWER_BOUND 1
#endif
// increment recurse count
#ifndef RECURSE_COUNT
#define RECURSE_COUNT 0
#elif RECURSE_COUNT == 0
#undef RECURSE_COUNT
#define RECURSE_COUNT 1
#elif RECURSE_COUNT == 1
#undef RECURSE_COUNT
#define RECURSE_COUNT 2
// ...
#endif
// expand macro with value
#if RECURSE_COUNT >= LOWER_BOUND \
&& RECURSE_COUNT <= UPPER_BOUND
MACRO(RECURSE_COUNT)
#endif
// re-include file
#if RECURSE_COUNT <= UPPER_BOUND
#include "file.h"
#else
#undef RECURSE_COUNT
#endif
#endif
Comeau C++ doesn't like this at all. It gives me a catastrophic error of a file
including itself. I tested including another file instead (file2.h) which just
included 'file.h', but I got the same error.
I didn't see anything in the standard regarding this either. For this example
above, it isn't really a big deal; I was just testing it out. I changed it to
the following (simpler) version:
// [file.h]
#ifndef UPPER_BOUND
#error UPPER_BOUND is not set
#endif
#ifndef LOWER_BOUND
#define LOWER_BOUND 1
#endif
#define RANGE_TEST(N) \
LOWER_BOUND <= N && UPPER_BOUND >= N
#if RANGE_TEST(0)
MACRO(0)
#endif
#if RANGE_TEST(1)
MACRO(1)
#endif
#if RANGE_TEST(2)
MACRO(2)
#endif
// etc.
#undef RANGE_TEST
This works fine for what I'm doing, and it is way more efficient. However, this
takes all the fun out of it. :) Actually, this goes up to 256, but don't worry,
I didn't type it out manually.
Paul Mensonides
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Paul Mensonides" <pmenso57@attbi.com>
Date: Fri, 5 Apr 2002 06:56:59 GMT Raw View
"James Kuyper Jr." <kuyper@wizard.net> wrote in message
news:3CAD18FF.4A96E@wizard.net...
> Paul Mensonides wrote:
> >
> > Is it illegal to have a file include itself if the recursion is bounded in
some
> > way?
>
> I believe so. I didn't find any prohibition on it in the obvious place
> (section 16.2), and I can't see any reason why it shouldn't be allowed.
> However, remember that there's an implementation-defined nesting limit
> on nested #includes, and a recursive #include also counts as a nested
> #include. The recommended minimum value for the nesting limit is 256,
> but a legal implementation could have a limit of 0, so long as it
> documents the fact.
I was just testing a file to expand a macro with a given series of numbers:
// [file.h]
#ifndef UPPER_BOUND
#error UPPER_BOUND is not set
#else
#ifndef LOWER_BOUND
#define LOWER_BOUND 1
#endif
// increment recurse count
#ifndef RECURSE_COUNT
#define RECURSE_COUNT 0
#elif RECURSE_COUNT == 0
#undef RECURSE_COUNT
#define RECURSE_COUNT 1
#elif RECURSE_COUNT == 1
#undef RECURSE_COUNT
#define RECURSE_COUNT 2
// ...
#endif
// expand macro with value
#if RECURSE_COUNT >= LOWER_BOUND \
&& RECURSE_COUNT <= UPPER_BOUND
MACRO(RECURSE_COUNT)
#endif
// re-include file
#if RECURSE_COUNT <= UPPER_BOUND
#include "file.h"
#else
#undef RECURSE_COUNT
#endif
#endif
Comeau C++ doesn't like this at all. It gives me a catastrophic error of a file
including itself. I tested including another file instead (file2.h) which just
included 'file.h', but I got the same error.
I didn't see anything in the standard regarding this either. For this example
above, it isn't really a big deal; I was just testing it out. I changed it to
the following (simpler) version:
// [file.h]
#ifndef UPPER_BOUND
#error UPPER_BOUND is not set
#endif
#ifndef LOWER_BOUND
#define LOWER_BOUND 1
#endif
#define RANGE_TEST(N) \
LOWER_BOUND <= N && UPPER_BOUND >= N
#if RANGE_TEST(0)
MACRO(0)
#endif
#if RANGE_TEST(1)
MACRO(1)
#endif
#if RANGE_TEST(2)
MACRO(2)
#endif
// etc.
#undef RANGE_TEST
This works fine for what I'm doing, and it is way more efficient. However, this
takes all the fun out of it. :) Actually, this goes up to 256, but don't worry,
I didn't type it out manually.
Paul Mensonides
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: David Schwartz <davids@webmaster.com>
Date: Fri, 5 Apr 2002 17:37:20 GMT Raw View
Paul Mensonides wrote:
> I was just testing a file to expand a macro with a given series of numbers:
How about this:
// This file is 'foo.h'
#ifndef FOO
#define FOO
#include "foo.h"
#endif
DS
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: "Paul Mensonides" <pmenso57@attbi.com>
Date: Fri, 5 Apr 2002 21:56:55 GMT Raw View
"David Schwartz" <davids@webmaster.com> wrote in message
news:3CADDB2E.C79840F8@webmaster.com...
> How about this:
>
> // This file is 'foo.h'
> #ifndef FOO
> #define FOO
> #include "foo.h"
> #endif
This works fine. I did some more testing, running Comeau C++ 4.3 BETA #1. If I
have a file like this:
// [file.h]
#ifndef UPPER_BOUND
#error UPPER_BOUND is not set
#endif
#ifndef LOWER_BOUND
#define LOWER_BOUND 1
#endif
#ifndef COUNTER
#define COUNTER 0
#elif COUNTER == 0
#undef COUNTER
#define COUNTER 1
#elif COUNTER == 1
#undef COUNTER
#define COUNTER 2
#elif COUNTER == 2
#undef COUNTER
#define COUNTER 3
// ... to 256
#elif COUNTER > 256
#error COUNTER overflow
#endif
#if COUNTER >= LOWER_BOUND && COUNTER <= UPPER_BOUND
MACRO(COUNTER)
#endif
#if COUNTER <= UPPER_BOUND
#include "file.h"
#else
#undef COUNTER
#endif
So theoretically, I can use it like this...
int main(int argc, char* argv[]) {
#define MACRO(N) std::cout << N << '\n';
#define UPPER_BOUND 5
#include "file.h"
endl(std::cout);
return 0;
}
Obviously, this should output:
1
2
3
4
5
And in this case it does, everything works fine. However, if I increase the
UPPER_BOUND past nine, I get a catastrophic error of the file including itself.
Apparently the limit here is nine.
Paul Mensonides
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]