From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19073 invoked by alias); 15 Nov 2011 09:30:33 -0000 Received: (qmail 19062 invoked by uid 22791); 15 Nov 2011 09:30:32 -0000 X-SWARE-Spam-Status: No, hits=-2.4 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,TW_PM X-Spam-Check-By: sourceware.org Received: from mail-ww0-f51.google.com (HELO mail-ww0-f51.google.com) (74.125.82.51) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 15 Nov 2011 09:30:12 +0000 Received: by wwe6 with SMTP id 6so5906608wwe.8 for ; Tue, 15 Nov 2011 01:30:10 -0800 (PST) MIME-Version: 1.0 Received: by 10.216.15.14 with SMTP id e14mr192365wee.21.1321349409541; Tue, 15 Nov 2011 01:30:09 -0800 (PST) Received: by 10.216.182.12 with HTTP; Tue, 15 Nov 2011 01:30:09 -0800 (PST) In-Reply-To: <4EC1FEF6.3050806@gmail.com> References: <4EC1789A.2070209@gmail.com> <4EC1FEF6.3050806@gmail.com> Date: Tue, 15 Nov 2011 19:55:00 -0000 Message-ID: Subject: Re: Template argument (pointer to member function) initialization with NULL From: Jonathan Wakely To: "vagran.ast" Cc: gcc-help@gcc.gnu.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org X-SW-Source: 2011-11/txt/msg00140.txt.bz2 On 15 November 2011 05:56, vagran.ast wrote: > On 11/14/2011 11:28 PM, Jonathan Wakely wrote: >> >> On 14 November 2011 20:22, vagran.ast wrote: >>> >>> Hi, >>> >>> In the following code: >>> >>> class A { >>> public: >>> =A0 =A0void SomeMethod() { } >>> }; >>> >>> template >>> class B { >>> >>> }; >>> >>> B =A0b1; // error: could not convert template argument '0' to 'void >>> (A::*)()' >>> >>> B =A0b2; // error: could not convert template argument '0' to 'vo= id >>> (A::*)()' >>> >>> void (A::*someMethod)() =3D 0; // OK >>> >>> there are two compilation errors. AFAIK per C++ standard 0 is a valid >>> value >>> for a pointer to member >>> function. Variable of such type can be successfully initialized by 0 but >>> template arguments can not. >>> Is it desired behavior or a bug? >> >> I think G++ is correct, you need to cast the literal zero to the right >> type to prevent it being treated as an int in that context, or use >> C++11's nullptr. > > I have tried both variants and still no luck: > > B(0)> b2; > // error: '((void (A::*)())0)' is not a valid template argument for type > 'void (A::*)()' > // error: it must be a pointer-to-member of the form '&X::Y' > // error: could not convert template argument '((void (A::*)())0)' to 'vo= id > (A::*)()' > // error: invalid type in declaration before ';' token > > B b3; // error: could not convert template argument 'nullptr'= to > 'void (A::*)()' > > void (A::*someMethod)() =3D static_cast(0);// OK > void (A::*someMethod2)() =3D nullptr;// OK > > Seems the compiler wants only real method pointer for a template parameter > however > the conversion from NULL to member function pointer is possible. That's a bug, maybe related to http://gcc.gnu.org/bugzilla/show_bug.cgi?id= =3D35167 Using nullptr works with GCC 4.7 I think this should work too: typedef void (A::*pmf_type)(); const pmf_type pmf_constant =3D 0; template class B { };