aschepler
11/5/2008 7:44:00 PM
On Nov 5, 2:12 pm, Pawel Dziepak <pdzie...@quarnos.org> wrote:
> Noah Roberts wrote:
> > template < typename T1, typename T2, T2 T1::*x >
> > struct test1
> > {
> > };
>
> > struct test2
> > {
> > int x;
>
> > typedef test1<test2, int, &test2::x> type;
> > };
>
> <snip>
>
> > Compiles with g++ and comeau online (warnings about unused variables)
> > but MS compiler gives error:
>
> <snip>
>
> 14.3.2/1:
> "[Template argument should be one of:] (...) the address of an object or
> function with external linkage, including function templates and
> function template-ids but *excluding non-static class members*"
>
> Compiler wouldn't know the address of test2::x at compile time, that's
> why such things are not allowed.
&test2::x does not have an address, it is a pointer to member value.
The very next portion of 14.3.2/1 specifically allows "a pointer to
member as described in 5.3.1" as a non-type template parameter.
I suspect this is indeed a compiler bug, but the details of when a
class member declaration may be referenced get tricky. To separate
the templating from the issue, I wonder if this would be a workaround,
or if not, I'd be curious on which line your compiler gives an error:
struct test2
{
int x;
static int test2::*const ptr_to_x = &test2::x;
typedef test1<test2, int, ptr_to_x> type;
};