 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Mikhail Parakhin Guest
|
Posted: Sun Sep 10, 2006 11:12 pm Post subject: Question about elaborate CRTP - is the friend declaration i |
|
|
{ Extranous blank lines (double-spacing) removed. -mod }
I'm using the following form of CRTP:
template<class P_This>
class T_Policy
{
protected:
void F() {static_cast<P_This*>(this)->G();}
};
template<template<class> class P_Policy>
class T_MyClass : public P_Policy<T_MyClass<P_Policy> >
{
friend class P_Policy<T_MyClass<P_Policy> >;
private:
void G() {}
};
Reason is that I want to give the user of T_MyClass an ability to change
policies and still don't want any virtual function overhead.
Now, my question is, does class P_Policy<T_MyClass<P_Policy> >
constitute an "elaborated type specifier"? In other words, can I
actually use the friend declaration like this? Because normal class-type
template parameters cannot be used in friend declaration.
Mikhail Parakhin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Gennaro Prota Guest
|
Posted: Mon Sep 11, 2006 4:17 am Post subject: Re: Question about elaborate CRTP - is the friend declaratio |
|
|
On 10 Sep 2006 14:12:20 -0400, "Mikhail Parakhin"
<parakhin (AT) comcast (DOT) net> wrote:
| Quote: | I'm using the following form of CRTP:
template<class P_This
class T_Policy
{
protected:
void F() {static_cast<P_This*>(this)->G();}
};
template<template<class> class P_Policy
class T_MyClass : public P_Policy<T_MyClass<P_Policy
{
friend class P_Policy<T_MyClass<P_Policy> >;
private:
void G() {}
};
Reason is that I want to give the user of T_MyClass an ability to change
policies and still don't want any virtual function overhead.
Now, my question is, does class P_Policy<T_MyClass<P_Policy
constitute an "elaborated type specifier"?
|
Yes, but the friend declaration is still well-formed C++03 :-)
In fact, the rule (relaxed in the current working paper) is that the
form "friend class typedef-name", which is the same as "friend class
identifier", is illegal, not any elaborated-type-specifier.
--
Gennaro Prota
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Mikhail Parakhin Guest
|
Posted: Mon Sep 11, 2006 6:29 pm Post subject: Re: Question about elaborate CRTP - is the friend declaratio |
|
|
Gennaro,
11.4/2 says: "An elaborated-type-specifier shall be used in a friend
declaration". So, ONLY elaborated-type-specifiers are permitted.
I know that
template<class T>
class C
{
friend class T;
};
is illigal, because class T is ill-formed. What I'm trying to understand is
whether friend class P_Policy<T_MyClass<P_Policy> >; is well formed
elaborated type specifier. It seams, that in case of template template
parameter it should be well-formed, and MSVC 8.0 compiles it - but I want to
be sure.
Mikhail
[I do not want replies, please followup to the group.]
"Gennaro Prota" <gennaro_prota (AT) yahoo (DOT) com> wrote in message
news:av09g2hk2o42o2oo6db04nir4vsitetnu8 (AT) 4ax (DOT) com...
| Quote: | On 10 Sep 2006 14:12:20 -0400, "Mikhail Parakhin"
parakhin (AT) comcast (DOT) net> wrote:
I'm using the following form of CRTP:
template<class P_This
class T_Policy
{
protected:
void F() {static_cast<P_This*>(this)->G();}
};
template<template<class> class P_Policy
class T_MyClass : public P_Policy<T_MyClass<P_Policy
{
friend class P_Policy<T_MyClass<P_Policy> >;
private:
void G() {}
};
Reason is that I want to give the user of T_MyClass an ability to change
policies and still don't want any virtual function overhead.
Now, my question is, does class P_Policy<T_MyClass<P_Policy
constitute an "elaborated type specifier"?
Yes, but the friend declaration is still well-formed C++03 :-)
In fact, the rule (relaxed in the current working paper) is that the
form "friend class typedef-name", which is the same as "friend class
identifier", is illegal, not any elaborated-type-specifier.
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Gennaro Prota Guest
|
Posted: Tue Sep 12, 2006 2:01 am Post subject: Re: Question about elaborate CRTP - is the friend declaratio |
|
|
On 11 Sep 2006 09:29:45 -0400, "Mikhail Parakhin"
<clcppm-poster (AT) this (DOT) is.invalid> wrote:
| Quote: | Gennaro,
11.4/2 says: "An elaborated-type-specifier shall be used in a friend
declaration". So, ONLY elaborated-type-specifiers are permitted.
|
Incidentally I can't find that in the current working paper, but
that's not the point.
| Quote: | I know that
template<class T
class C
{
friend class T;
};
is illigal, because class T is ill-formed.
|
No, "class T" is not ill-formed, "friend class T" is. See 7.1.5.3/2.
| Quote: | What I'm trying to understand is
whether friend class P_Policy<T_MyClass<P_Policy> >; is well formed
elaborated type specifier. It seams, that in case of template template
parameter it should be well-formed, and MSVC 8.0 compiles it - but I want
to
be sure.
|
I believe it is valid. I can't see any sentence analogous to the one
in 7.1.5.3/2 to make it ill-formed (and if such a sentence existed why
not putting it in the same section --but the standard does have such
oddities, where logically related statements are scattered in
different places without explicit links).
--
Gennaro Prota
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Gennaro Prota Guest
|
Posted: Tue Sep 12, 2006 2:01 am Post subject: Re: Question about elaborate CRTP - is the friend declaratio |
|
|
On 10 Sep 2006 19:17:00 -0400, Gennaro Prota <gennaro_prota (AT) yahoo (DOT) com>
wrote:
| Quote: | On 10 Sep 2006 14:12:20 -0400, "Mikhail Parakhin"
parakhin (AT) comcast (DOT) net> wrote:
Now, my question is, does class P_Policy<T_MyClass<P_Policy
constitute an "elaborated type specifier"?
Yes, but the friend declaration is still well-formed C++03 :-)
In fact, the rule (relaxed in the current working paper) is that the
form "friend class typedef-name", which is the same as "friend class
identifier", is illegal, not any elaborated-type-specifier.
|
Some sloppy editing here, sorry. I meant: "the form 'friend class
identifier' is illegal when identifier resolves to a typedef-name.
Other forms, such as the one in the OP example, are valid
elaborated-type-specifiers".
--
Gennaro Prota
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Thu Sep 14, 2006 5:15 pm Post subject: Re: Question about elaborate CRTP - is the friend declaratio |
|
|
Gennaro Prota ha scritto:
| Quote: | No, "class T" is not ill-formed, "friend class T" is. See 7.1.5.3/2.
|
7.1.5.3/2 says "If the identifier resolves to a typedef-name or a
template type-parameter, the elaborated-type-specifier is ill-formed."
So I think the OP is right, the elaborated-type-specifier "class T" is
ill-formed, and that in turn makes the declaration "friend class T"
ill-formed as well.
| Quote: | What I'm trying to understand is
whether friend class P_Policy<T_MyClass<P_Policy> >; is well formed
elaborated type specifier. It seams, that in case of template template
parameter it should be well-formed, and MSVC 8.0 compiles it - but I want
to
be sure.
I believe it is valid. I can't see any sentence analogous to the one
in 7.1.5.3/2 to make it ill-formed (and if such a sentence existed why
not putting it in the same section --but the standard does have such
oddities, where logically related statements are scattered in
different places without explicit links).
|
P_Policy<T_MyClass<P_Policy> > is a template-id (14.2/1) so 7.1.5.3/1
explicitly says the declaration is s well-formed (it's the third case).
The fact that we have template template parameters is irrelevant, even
with non-template template parameters it would be ok.
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Mikhail Parakhin Guest
|
Posted: Thu Sep 14, 2006 9:49 pm Post subject: Re: Question about elaborate CRTP - is the friend declaratio |
|
|
| Quote: | 7.1.5.3/2 says "If the identifier resolves to a typedef-name or a
template type-parameter, the elaborated-type-specifier is ill-formed."
So I think the OP is right, the elaborated-type-specifier "class T" is
ill-formed, and that in turn makes the declaration "friend class T"
ill-formed as well.
|
I totally agree. That was my understanding as well
| Quote: | The fact that we have template template parameters is irrelevant, even
with non-template template parameters it would be ok.
|
I don't quite understand what you mean. My understanding is that template
template parameter is the gist here. If it wasn't template template
parameter, it would be something like
template <class P_Policy>
class T_MyClass
{
friend class P_Policy; // Illegal - resolves to "template
type-parameter"!
};
With template template parameter it is
template <template<class >class P_Policy>
class T_MyClass
{
friend class P_Policy<T_MyClass<P_Policy> >; // seams to be legal,
because it is not a "template type-parameter", but "template template
parameter".
};
Mikhail Parakhin
[I do not want replies, please followup to the group.]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Sat Sep 16, 2006 5:15 am Post subject: Re: Question about elaborate CRTP - is the friend declaratio |
|
|
Mikhail Parakhin ha scritto:
| Quote: | The fact that we have template template parameters is irrelevant, even
with non-template template parameters it would be ok.
I don't quite understand what you mean. My understanding is that template
template parameter is the gist here. If it wasn't template template
parameter, it would be something like
template <class P_Policy
class T_MyClass
{
friend class P_Policy; // Illegal - resolves to "template
type-parameter"!
};
|
Oh, now I understand your point. That's illegal, but not because
"P_Policy" is a *type* parameter, but simply because it is the name of a
parameter *used alone*. But if the parameter is used as part of a
template-id, then it's legal:
template <template<class >class P_Policy>
class T_MyClass
{
friend class P_Policy<T_MyClass<P_Policy> >; // legal!
};
template <class P_Policy>
class T_MyClass
{
friend class AnyTemplate<P_Policy>; // legal!
};
They're both legal because "P_Policy<T_MyClass<P_Policy> >" and
"AnyTemplate<P_Policy>" are template-ids.
Ganesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|