From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 20491 invoked by alias); 10 Jul 2009 12:44:18 -0000 Received: (qmail 20474 invoked by uid 22791); 10 Jul 2009 12:44:17 -0000 X-SWARE-Spam-Status: No, hits=-1.5 required=5.0 tests=AWL,BAYES_00,J_CHICKENPOX_13,SARE_MSGID_LONG40,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mail-vw0-f198.google.com (HELO mail-vw0-f198.google.com) (209.85.212.198) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 10 Jul 2009 12:44:09 +0000 Received: by vwj36 with SMTP id 36so682508vwj.0 for ; Fri, 10 Jul 2009 05:44:06 -0700 (PDT) MIME-Version: 1.0 Received: by 10.220.94.69 with SMTP id y5mr2831189vcm.6.1247229846891; Fri, 10 Jul 2009 05:44:06 -0700 (PDT) In-Reply-To: <4d623d70907100448g529a565qc64371440c15a7c8@mail.gmail.com> References: <4d623d70907100448g529a565qc64371440c15a7c8@mail.gmail.com> Date: Fri, 10 Jul 2009 12:44:00 -0000 Message-ID: <84fc9c000907100544n6d0a3d6u3927eb9925d1110@mail.gmail.com> Subject: Re: Possible bug in g++ - template specialization From: Richard Guenther To: Maciej Cencora Cc: gcc@gcc.gnu.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org X-SW-Source: 2009-07/txt/msg00186.txt.bz2 On Fri, Jul 10, 2009 at 1:48 PM, Maciej Cencora wrote: > Hi, > > I think I've found a bug in g++. Let's say we have following files: > > // a.hpp > > template > void func1() > { > =A0 =A0 =A0 =A0// general code > } > > // a.cpp > > #include "a.hpp" > > template<> > void func1() > { > =A0 =A0 =A0 =A0// specialized code > } > > // main.cpp > > #include "a.hpp" > > int main(void) > { > =A0 =A0 =A0 =A0func1(); > > =A0 =A0 =A0 =A0return 0; > } > > > Now when we run the program compiled with: g++ main.cpp a.cpp -o main > specialized version of func1 will be called, but when compiled with > -Os flag the general version will be called. > I'm not an expert but I believe the -Os behaviour is the correct one. By not making the specialized version available at the point of instantiation you are violating the one-definition rule (ODR, no need to diagnose it) and the behavior is undefined. Richard.