This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

C++ bug in 1.1.1 prerelease, could we fix it in 1.1.1?



When using templates one wants to have a way of generating an compile time
error if the template parameters are wrong. One way of doing it is as
follows:

#define COMPILER_ASSERT(cond) sizeof(int[(cond)?1:0])

Any ANSI compiler must give an error when the macro is expanded with
cond=='false' because the integer array will have zero length. Since gnu
gcc doesn't complain on this unless compiled with --ansi --pedantic
(and then only warns) one could change the above into

#define COMPILER_ASSERT(cond) sizeof(int[(cond)?1:-1])

Now, negative sized arrays are not allowed in egcs, but there is a nasty
bug in all releases and snapshots I've seen so far (including 1.1.1
prerelease) that prevents checking of array sizes inside template
declarations, so for example egcs doesn't even complain on the following
code:

template<int N>
class foo {
public:
  int x[-1];
};

main() {
  foo<1> bar;
}

No error, not even with --ansi --pedantic!

I think this is a serious problem. Couldn't we try to correct this in the
1.1.1 release?

I think (warning! dr Watson's speaking) that the problem is that array
size checking is always deferred inside template declarations, but then
never performed at instantiation, perhaps because the compiler confuses
this with the variable-sized-array extension(?)

Regards
/Oskar

*/              Oskar Enoksson, Linkoping, Sweden                  /*



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]