From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27380 invoked by alias); 27 Feb 2008 17:36:14 -0000 Received: (qmail 27187 invoked by uid 48); 27 Feb 2008 17:35:28 -0000 Date: Wed, 27 Feb 2008 17:36:00 -0000 Subject: [Bug c++/35394] New: Agressive template instantiation? X-Bugzilla-Reason: CC Message-ID: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "proy at clg dot qc dot ca" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2008-02/txt/msg02866.txt.bz2 I think recent versions of g++ (4.*, for example, but not 3.4.4) go too far performing C++ template instantiation. Mind you, this is only a guess, and you guys will know better. Here's the problem case. The code shown below should (in my opinion) compile properly as is but should not compile when the 2nd line of main() is not commented out. This «non-compilability» of the 2nd line is made on purpose, to simplify static error checking, and relies on a technique similar to the one made by boost's static assert on most platforms. The intent is to cause meaningful error messages in cases where we can "see them coming", without preventing correct programs to compile. In main(), the 1st line is expected to be legal and the 2nd (when it's not commented out) is illegal. What I see as a bug here is that the code for generic class not_compilable seems to be considered even when the code does not use it. In more detail: the goal of the test cas below is to provoke a compile-time failure that describes the error using the template parameter Reason in generic class not_compilable, but only when the static int value used for factorial is negative. This test code compiles (or does not compile, depending on the case) fine using most compilers at my disposal (g++ 3.3* and 3.4* included) but most of my students using g++ 4.* report that this code fails to compile even when only the 1st line of main() is there. I would like to give you guys more info but all g++ compilers around me seem to handle this correctly; only "recent versions" (at least from 4.1* on) seem to mishandle it (at least if one considers, as I do, the following technique to be valid C++). The code follows... // ------------------- // // static_assert is provided here for clarity; remove it or // rename it if it is provided by your compiler // template struct static_assert; template <> struct static_assert {}; // // A class made not to compile, on purpose. Since it is generic, my // expectation is that it should not be considered if it is not being // used. It is syntactically correct and compiles fine (when not used) // on older g++ (3.3* to 3.4* at least) and on all compilers at my // disposal, as mentioned above // template class not_compilable { enum { dummy_value = sizeof (static_assert) }; }; // // A class that does compile :) // struct compilable { }; // // A static type selector // template struct static_if_else; template struct static_if_else { typedef IfTrue type; }; template struct static_if_else { typedef IfFalse type; }; // // An empty class to add meaning when factorial does // not compile using a negative value for N // class negative_value_not_accepted {}; // // A class that should only compile when N>=0 and that // should fail otherwise // template struct factorial : private static_if_else< (N<0), not_compilable, compilable >::type { enum { value = N * factorial::value }; }; template <> struct factorial<0> { enum { value = 1 }; }; // // The test program. My understanding is that it should compile // when only the 1st line is included and that it should not // compile when the 2nd line is included. What I see as a bug is // that g++ 4.1*+ seems not to accept even the 1st line, considering // not_compilable even though it is not being used by the program... // #include using std::cout; int main () { cout << factorial<5>::value; // Cool; should compile //cout << factorial<-3>::value; // should not compile } -- Summary: Agressive template instantiation? Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: proy at clg dot qc dot ca http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35394