 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
frs Guest
|
Posted: Thu Aug 25, 2005 3:42 am Post subject: templated member funcs in templated member funcs |
|
|
Does the standard forbid templated member functions to
call themselves templated member functions from its members?
Gcc 3.3.1 does not allow it and gives a syntax error. I.e.
in the example below print_1() works, print_2() does not.
Are there compilers that do it. Thanks for any comments
on that issue.
Best Regards,
Frank.
EXAMPLE: --------------------------------------------------------------
class B {
public:
template <typename T> void hallo();
};
template<> void B::hallo<int>() { cout << "
template<> void B::hallo<double>(){ cout << "
template
template<> void Hallo<int>() { cout << "
template<> void Hallo<double>() { cout << "
class A {
public:
template
template <typename T> void print_2();
B b;
};
template<typename T> void A::print_1() { Hallo<T>(); }
template<typename T> void A::print_2() { b.hallo<T>(); }
---------------------------------------------------------------------------
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Aug 25, 2005 3:42 pm Post subject: Re: templated member funcs in templated member funcs |
|
|
frs wrote:
| Quote: | Does the standard forbid templated member functions to
call themselves templated member functions from its members?
Gcc 3.3.1 does not allow it and gives a syntax error. I.e.
in the example below print_1() works, print_2() does not.
Are there compilers that do it. Thanks for any comments
on that issue.
Best Regards,
Frank.
EXAMPLE: --------------------------------------------------------------
class B {
public:
template <typename T> void hallo();
};
template<> void B::hallo<int>() { cout << "
template<> void B::hallo<double>(){ cout << "
template
template<> void Hallo<int>() { cout << "
template<> void Hallo<double>() { cout << "
class A {
public:
template
template <typename T> void print_2();
B b;
};
template<typename T> void A::print_1() { Hallo<T>(); }
template<typename T> void A::print_2() { b.hallo<T>(); }
|
This needs to be
template<typename T> void A::print_2() { b.template hallo<T>(); }
if I read the Standard correctly.
V
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Zara Guest
|
Posted: Fri Aug 26, 2005 4:25 am Post subject: Re: templated member funcs in templated member funcs |
|
|
Change the definition of print_2:
template<typename T> void A::print_2() { b.template hallo<T>(); }
This ".template" avoids the compiler interpreting the opening '<' as a
less-than comparator.
I also think this is sorcery, so there is the reference:
C++ Templates (Vandevoorde-Josuttis), chapter 5, "Tricky Basics"
I have been unable to find if in ISO C++ std, but if it is explained in
that book, it is most probable it is standard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
google@vandevoorde.com Guest
|
Posted: Sat Aug 27, 2005 6:50 am Post subject: Re: templated member funcs in templated member funcs |
|
|
Zara wrote:
| Quote: | Change the definition of print_2:
template<typename T> void A::print_2() { b.template hallo<T>(); }
This ".template" avoids the compiler interpreting the opening '<' as a
less-than comparator.
I also think this is sorcery, so there is the reference:
C++ Templates (Vandevoorde-Josuttis), chapter 5, "Tricky Basics"
I have been unable to find if in ISO C++ std, but if it is explained in
that book, it is most probable it is standard
|
FWIW, the reference in the standard is 14.2 [temp.names]/4.
In this case "b" is has a template dependent type; so it is indeed
required.
Daveed
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Sat Aug 27, 2005 5:59 pm Post subject: Re: templated member funcs in templated member funcs |
|
|
[email]google (AT) vandevoorde (DOT) com[/email] wrote:
| Quote: | Zara wrote:
Change the definition of print_2:
template<typename T> void A::print_2() { b.template hallo<T>(); }
This ".template" avoids the compiler interpreting the opening '<' as a
less-than comparator.
I also think this is sorcery, so there is the reference:
C++ Templates (Vandevoorde-Josuttis), chapter 5, "Tricky Basics"
I have been unable to find if in ISO C++ std, but if it is explained in
that book, it is most probable it is standard
FWIW, the reference in the standard is 14.2 [temp.names]/4.
In this case "b" is has a template dependent type; so it is indeed
required.
|
Maybe I am missing something here, but... b doesn't look dependent at
all to me, as both A and B are a non-template classes. In fact all of
Comeau Online, VC7.1 and gcc 3.3.3 agree to compile the OP's code
without the ".template" syntax. Could it be just a gcc 3.3.1 bug?
Alberto
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Zara Guest
|
Posted: Mon Aug 29, 2005 7:50 am Post subject: Re: templated member funcs in templated member funcs |
|
|
| Quote: | Zara wrote
(...)
template<typename T> void A::print_2() { b.template hallo<T>(); }
(...)
|
Alberto Barbati wrote
| Quote: | (...)
Maybe I am missing something here, but... b doesn't look dependent at
all to me, as both A and B are a non-template classes.
(...)
|
It is not b which is dependant, it is hallo the dependant one, as the
instantiation refers to <T>, which is a template parameter.
[Reposted with context references, as suggested by group moderator.
Thank's for the point]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Mon Aug 29, 2005 3:54 pm Post subject: Re: templated member funcs in templated member funcs |
|
|
Zara wrote in news:1125297398.499345.184950 (AT) g44g2000cwa (DOT) googlegroups.com
in comp.std.c++:
| Quote: | Zara wrote
(...)
template<typename T> void A::print_2() { b.template hallo<T>(); }
(...)
Alberto Barbati wrote
(...)
Maybe I am missing something here, but... b doesn't look dependent at
all to me, as both A and B are a non-template classes.
(...)
It is not b which is dependant, it is hallo the dependant one, as the
instantiation refers to <T>, which is a template parameter.
|
hallo isn't a "dependent name", to find hallo and to know that it
is a member template only requires knowledge of b which isn't a
dependant name.
Note what matters is wether the *name* is dependant, instantiations are
always dependant on there paramiters, so there is nothing unusual here.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
kuyper@wizard.net Guest
|
Posted: Mon Aug 29, 2005 4:00 pm Post subject: Re: templated member funcs in templated member funcs |
|
|
Alberto Barbati wrote:
| Quote: | google (AT) vandevoorde (DOT) com wrote:
Zara wrote:
Change the definition of print_2:
template<typename T> void A::print_2() { b.template hallo<T>(); }
This ".template" avoids the compiler interpreting the opening '<' as a
less-than comparator.
..
FWIW, the reference in the standard is 14.2 [temp.names]/4.
In this case "b" is has a template dependent type; so it is indeed
required.
Maybe I am missing something here, but... b doesn't look dependent at
all to me, as both A and B are a non-template classes. In fact all of
Comeau Online, VC7.1 and gcc 3.3.3 agree to compile the OP's code
without the ".template" syntax. Could it be just a gcc 3.3.1 bug?
|
[email]google (AT) vandevoorde (DOT) com[/email] came to the right conclusion, but specified the
wrong reason. It's not "b" which is dependent, it's "b.hallo
The relevant clause (14.2p4) says:
"When the name of a member template specialization appears after . or
-> in a _postfix-expression_, or after _nested-name-specifier_ in a
_qualified-id_, and the _postfix-expression_ or _qualified-id_
explicitly depends on a template-parameter (14.6.2), the member
template name must be prefixed by the keyword template."
Note that it's the entire postfix-expression that must be checked for
dependence, not just the the part prior to the '.'. The
postfix-expression in this case is "b.hallo<T>", and it is quite
explicitly dependent on the template parameter T, even though "b"
itself is not dependent on any template parameter.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
google@vandevoorde.com Guest
|
Posted: Mon Aug 29, 2005 4:58 pm Post subject: Re: templated member funcs in templated member funcs |
|
|
Alberto Barbati wrote:
| Quote: | google (AT) vandevoorde (DOT) com wrote:
[...]
FWIW, the reference in the standard is 14.2 [temp.names]/4.
In this case "b" is has a template dependent type; so it is indeed
required.
Maybe I am missing something here, but... b doesn't look dependent at
all to me, as both A and B are a non-template classes. In fact all of
Comeau Online, VC7.1 and gcc 3.3.3 agree to compile the OP's code
without the ".template" syntax. Could it be just a gcc 3.3.1 bug?
|
You're right of course. I should have actually read the whole
example :-P
g++ 3.4 accepts the code "as is" (well, with some additions to
resolve the iostream uses).
Daveed
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
kuyper@wizard.net Guest
|
Posted: Tue Aug 30, 2005 3:41 am Post subject: Re: templated member funcs in templated member funcs |
|
|
Rob Williscroft wrote:
| Quote: | Zara wrote in news:1125297398.499345.184950 (AT) g44g2000cwa (DOT) googlegroups.com
in comp.std.c++:
..
It is not b which is dependant, it is hallo the dependant one, as the
instantiation refers to <T>, which is a template parameter.
hallo isn't a "dependent name", to find hallo and to know that it
is a member template only requires knowledge of b which isn't a
dependant name.
Note what matters is wether the *name* is dependant, instantiations are
always dependant on there paramiters, so there is nothing unusual here.
|
The relevant clause says nothing about the name being dependent. The
only thing it says about the name of the template member specialization
is that it occurs after the '.' or '->' in a postfix-expression, or
after the nested-name-specifier in a qualified-id. It's the dependency
of the postfix-expression or the qualified-id containing the name that
renders the 'template' keyword mandatory. As long as the
postfix-expression is dependent, it doesn't matter whether the template
member's name is dependent.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
google@vandevoorde.com Guest
|
Posted: Tue Aug 30, 2005 3:12 pm Post subject: Re: templated member funcs in templated member funcs |
|
|
[email]kuyper (AT) wizard (DOT) net[/email] wrote:
[...]
| Quote: | google (AT) vandevoorde (DOT) com came to the right conclusion, but specified the
wrong reason. It's not "b" which is dependent, it's "b.hallo<T>".
|
No, I just jumped to conclusions. The code is essentially fine as is.
| Quote: | The relevant clause (14.2p4) says:
"When the name of a member template specialization appears after . or
-> in a _postfix-expression_, or after _nested-name-specifier_ in a
_qualified-id_, and the _postfix-expression_ or _qualified-id_
explicitly depends on a template-parameter (14.6.2), the member
template name must be prefixed by the keyword template."
Note that it's the entire postfix-expression that must be checked for
dependence, not just the the part prior to the '.'. The
postfix-expression in this case is "b.hallo<T>", and it is quite
explicitly dependent on the template parameter T, even though "b"
itself is not dependent on any template parameter.
|
There is a known wording defect in this paragraph (Core issue 431).
The note in the next paragraph (14.2/5) makes it quite clear that
the second use of "postfix-expression" in 14.2/4 was meant to
refer to the subconstruct preceding the "." or "->".
Daveed
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
kuyper@wizard.net Guest
|
Posted: Wed Aug 31, 2005 6:20 am Post subject: Re: templated member funcs in templated member funcs |
|
|
[email]google (AT) vandevoorde (DOT) com[/email] wrote:
| Quote: | kuyper (AT) wizard (DOT) net wrote:
..
The relevant clause (14.2p4) says:
"When the name of a member template specialization appears after . or
-> in a _postfix-expression_, or after _nested-name-specifier_ in a
_qualified-id_, and the _postfix-expression_ or _qualified-id_
explicitly depends on a template-parameter (14.6.2), the member
template name must be prefixed by the keyword template."
Note that it's the entire postfix-expression that must be checked for
dependence, not just the the part prior to the '.'. The
postfix-expression in this case is "b.hallo<T>", and it is quite
explicitly dependent on the template parameter T, even though "b"
itself is not dependent on any template parameter.
There is a known wording defect in this paragraph (Core issue 431).
The note in the next paragraph (14.2/5) makes it quite clear that
the second use of "postfix-expression" in 14.2/4 was meant to
refer to the subconstruct preceding the "." or "->".
|
That sounds plausible. Except for one serious problem - the note you
refer to, and the note that is referenced in Core Issue 431, is not
present in my copy of the standard. There is a note in 14.2p5, but it's
text is quite different from what is cited in Core Issue 431: "[_Note_:
the keyword template may not be applied to non-template members of
class templates.]". This isn't just a simple mis-citation; I searched
the entire document for text matching the citation in 431, and couldn't
find it.
Was this text added after the 1998 release of the standard? I don't
have copy that reflects any of those changes.
As far as I can from the web site, there's one person who agrees with
the defect report, and no one else who's expressed an opinion. The
status of 431 is listed as "Drafting". What exactly does that mean?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| Back to top |
|
 |
google@vandevoorde.com Guest
|
Posted: Thu Sep 01, 2005 6:45 am Post subject: Re: templated member funcs in templated member funcs |
|
|
[email]kuyper (AT) wizard (DOT) net[/email] wrote:
[...]
| Quote: | There is a known wording defect in this paragraph (Core issue 431).
The note in the next paragraph (14.2/5) makes it quite clear that
the second use of "postfix-expression" in 14.2/4 was meant to
refer to the subconstruct preceding the "." or "->".
That sounds plausible. Except for one serious problem - the note you
refer to, and the note that is referenced in Core Issue 431, is not
present in my copy of the standard. There is a note in 14.2p5, but it's
text is quite different from what is cited in Core Issue 431: "[_Note_:
the keyword template may not be applied to non-template members of
class templates.]". This isn't just a simple mis-citation; I searched
the entire document for text matching the citation in 431, and couldn't
find it.
Was this text added after the 1998 release of the standard? I don't
have copy that reflects any of those changes.
|
Ah yes. I just checked the diffs between the 1998 version and
the 2003 version (aka. TC1) of the standard and you're right.
These words were added during that time (apparently for the
resolution of core issue 30).
| Quote: | As far as I can from the web site, there's one person who agrees with
the defect report, and no one else who's expressed an opinion. The
status of 431 is listed as "Drafting". What exactly does that mean?
|
"Drafting" status means that there is a consensus among the active
participants in the Core working group and that someone has been
assigned to propose specific wording changes to implement that
consensus.
The defect report notes appears to only reflect the e-mail discussion
that
took place. At the time, only John Spicer responded (he is sometimes
called "Mr. Template" -- when he responded, probably nobody felt the
need to add anything). Note the "Priority 0": That means that the
issue
is considered "obvious" in some way (which is mostly likely why the
notes
don't contain additional discussion beyond the e-mail capture). I.e.,
at a
face-to-face meeting the Core WG decided that the report was obviously
right and that the resolution was "easy" -- John was then assigned the
task of writing it up.
Daveed
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
|
|
| 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
|
|