Bug with null pointer to member as template argument

David Mazieres dm@reeducation-labor.lcs.mit.edu
Wed Dec 16 20:16:00 GMT 1998


> Date: Wed, 16 Dec 1998 16:14:18 -0800
> From: Mark Mitchell <mark@markmitchell.com>
> 
> 
>     David>   template<class T, int T::*field> struct list {};
>   
>     David>   struct foo { int x; list<foo, 0> subfoos; };

You are right.  I was mistaken.

For anyone else browsing the list and puzzling over the example,
however, let me give a slightly different explanation.

> You missed:
> 
>   A template-argument for a non-type, non-template
>        template-parameter shall be one of:
> 
>        ... 
> 
>        --a pointer to member expressed as described in _expr.unary.op_. 

But it also allows

       an integral constant-expression of integral or enumeration type...

Thus, "0" is a perfectly legal template argument.  The question is
which corresponding template *parameters* is it valid for.

> which says that you must write `&C::f' for some choice of `C' and
> `f'.  Also:
> 
>  For a non-type template-parameter of type pointer to member
> 	   function, no conversions apply.

In fact, this clause explicitly applies to pointer to member
*functions*, not pointer to *data* members.  This is what initially
threw me off.  The next bullet is the relevant one:

  For a non-type template-parameter of type pointer to data member,
  qualification conversions (4.4) are applied.

In order to make "0" a pointer to data member, however, you need the
coversion from section 4.11, not 4.4, so by omission 4.11 is not
permitted.  In fact, the pointer to object bullet makes this a lot
clearer:

  for a non-type template-parameter of type pointer to object,
  qualification conversions (4.4) and the array-to-pointer conversion
  (4.2) are applied. [Note: In particular, neither the null pointer
  conversion (4.10) nor the derived-to-base conversion (4.10) are
  applied. Although 0 is a valid template-argument for a non-type
  template-parameter of integral type, it is not a valid
  template-argument for a non-type template-parameter of pointer
  type. ]

Thanks for the response!

David



More information about the Gcc-bugs mailing list