Pointer to member contravariance and template non-type arguments

59 views
Skip to first unread message

Joaquín M López Muñoz

unread,
Jan 10, 2006, 3:50:29 AM1/10/06
to
The following piece of code is legal C++:

struct A
{
int x;
};

struct B:A{};

int main()
{
int B::* p=&B::x;
}

which is reasonable enough, as A::x is part of B public interface via
inheritance. Perhaps a little metaphorically, we can regard this
aliasing of a pointer to member of base as a pointer to
member of derived like a sort of contravariance rule.
But in the context of template non-type arguments, the rule does not
apply:

struct A
{
int x;
};

struct B:A{};

template<int B::*Ptr>
struct foo{};

int main()
{
// error: argument of type "int A::*" is incompatible with
// template parameter of type "int B::*"
foo<&B::x> f;
}

I'm sure there's some place in the standard implicitly or explicitly
banning the conversion in this particular context (though I couldn't
find it). My questions are:

* Is there any sensible reason for this banning that escapes me?
Why here (template args) and not anywhere else?
* If not, would it be a good idea to file a DR?

Thanks in advance,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std...@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

vervae...@googlemail.com

unread,
Jul 30, 2015, 2:20:06 PM7/30/15
to

This is acutallly a really good question. Too bad it didn't get the answer
it deserved.

Op dinsdag 10 januari 2006 09:50:29 UTC+1 schreef Joaqu=C3=ADn M L=C3=B3pez=
Mu=C3=B1oz:
> Joaqu=C3=ADn M L=C3=B3pez Mu=C3=B1oz
> Telef=C3=B3nica, Investigaci=C3=B3n y Desarrollo
>
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std...@ncar.ucar.edu ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use mailto:std-cpp...@vandevoorde.com ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Reply all
Reply to author
Forward
0 new messages