The following C++ program aims to calculate the sum of some ints at compile
time:
//BEGINS SUM.CPP
template<int N>
class Sum {
public:
enum { val = N };
template<int K>
class Inc : public Sum<val + K> { };
};
int main() {
return(Sum<2>::Inc<3>::Inc<6>::Inc<4>::val);
}
//ENDS SUM.CPP
Now GCC produces:
$ g++ sum.c
$ ./a.out ; echo $?
6
More generally, every Sum<x_0>::Inc<x_1>::Inc<x_2>::...::Inc<x_n>::val is
always expanded to just x_0 + x_n. When compiled with Digital Mars C++ the same
example produces 15 as output (more generally, produces the sum of all the
provided integers as I would expect).
Which is the right behaviour?
---
$ g++ -v
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: ./gcc-4.3.3/configure
Thread model: posix
gcc version 4.3.3 (GCC)
I checked this example against a barebone, fresh 4.3.3 build with just gcc-core
and gcc-g++, gmp-4.2.1 and mpfr-2.3.0, all downloaded from a gcc mirror, fully
boostrapped with no options on a Gentoo distribuition. The same behaviour
occurs with a 4.1.2 GCC version on Gentoo and with a 3.4.4 GCC version on
Cygwin. The Digital Mars C++ compiler version I used is 8.50 for win32.
--
Summary: Template behaviour when a template class has a member
template class deriving from its container
Product: gcc
Version: 4.3.3
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: pietro dot braione at polimi dot it
GCC target triplet: x86_64-unknown-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39777