Boost logo

Boost :

From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2007-03-19 18:38:17


Ion Gaztañaga wrote:
> I mean:
>
> template<class T, member_hook T::* P>
> class value_traits;
>
> compiles fine, but I just want to have one template parameter
>
> template<member_hook T::* P>
> class value_traits;
>
> This is a compilation error. Can C++ deduce both the parent class and
> the pointer to member? I don't see any reason that should forbid this,
> but I can find the right syntax.
>
There is none. There's no such thing as implicit template parameters.
Each parameter must be explicitly specified.

P is a non-type template parameter; it takes a constant value, not a
type. The constant value is a pointer-to-member. (Typically an offset
inside a class, but of course that's implementation-dependent.) A
pointer-to-member needs a class it points into.
In your case, which class is it? You only have T there, so the compiler
looks for a type called T. Not finding one, it throws an error.
If there was an implicit template parameter there, that would make the
code extremely fragile: as long as no type T is defined at the point of
the template declaration, the template has two parameters - probably the
compiler would, not finding a type T, assume it's an implicit template
parameter.
If at any point in time a type T is made visible to the template
declaration, the meaning suddenly changes.

This is of course unacceptable. And it works the other way, too. Consider:

struct FooBarZoot { ... };

template <int FoobarZoot::* P>
class Oops { ... };

How many template parameters does Oops have? That's right, two, because
FoobarZoot is different from FooBarZoot and thus an implicit template
parameter. The error message would be very helpful, too.

As you can see, it's imperative that every template parameter is
specified explicitly.

Sebastian Redl


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk