Bug 39777 - Template behaviour when a template class has a member template class deriving from its container
Summary: Template behaviour when a template class has a member template class deriving...
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.3
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-04-15 15:18 UTC by Pietro Braione
Modified: 2011-10-19 01:51 UTC (History)
1 user (show)

See Also:
Host:
Target: x86_64-unknown-linux-gnu
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
the example program (137 bytes, text/plain)
2009-04-15 15:20 UTC, Pietro Braione
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Pietro Braione 2009-04-15 15:18:37 UTC
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.
Comment 1 Pietro Braione 2009-04-15 15:20:56 UTC
Created attachment 17643 [details]
the example program
Comment 2 Andrew Pinski 2009-04-15 15:39:30 UTC
The issue comes down to how is Inc injected into Inc.  I can't remember the exact rules but I think GCC's behavior is correct.
Comment 3 Paolo Carlini 2009-04-15 17:04:21 UTC
For the record, the Intel compiler - which I trust much more than DM, sorry - behaves the same as GCC. I strongly believe GCC is correct. I even think we have something closed about this issue, let me see if I find it...
Comment 4 Paolo Carlini 2011-10-19 01:51:38 UTC
This is even rejected now.