Topic: default template parameters
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Mon, 23 Jul 2001 23:58:19 GMT Raw View
William Jeremiah wrote:
>
> I have a question. My compiler:
>
> /opt/aCC/bin/aCC -V
> aCC: HP ANSI C++ B3910B A.03.13
>
> Produces these error messages:
>
> template <int X, int Y = 1, int Z = X> struct Root;
>
> Error 206: "x.cpp", line 2
> Cannot cast from void expression.
> template <int X, int Y = 1, int Z = X> struct Root;
> ^^^^^
>
> int A = 1;
> template <int X, int Y = 1, int Z = A> struct Root;
>
> Error 519: "x.cpp", line 4
> Non-type template arguments must be constant
> expressions, addresses of objects or functions
> with external linkage, or static class members.
> template <int X, int Y = 1, int Z = A> struct Root;
> ^^^^^
>
> These error messages do not seem to be valid
> to me. According to the standard (actually
> I am working off the Dec96 Draft at
> http://www.cygnus.com/misc/wp/dec96pub
> mostly because I can't afford the real thing)
How long does it take for you to earn $18? How much time do you spend
reading the standard? If the answer to the second question is much
larger than the answer to the first, you can't afford to not buy it;
you'll end up wasting valuable time tracking down incorrect citations.
> a default template argument can be a value (14.1.8)
> and if you follow the grammar from template
> parameter (14.1.1) down to unqualified-id (5.1.6)
> apparently any identifier can be used as a default
> parameter.
Analyzing the grammar isn't sufficient; the grammar captures only one
part of what is needed to make a well-formed C++ program.
> My question is, since I don't have the actual
> Standard, are these two constructs actually
> valid C++?
The relevant question is not a matter of the syntax, but of semantics.
The scope of 'X' in the first case includes the default argument of 'Z';
there's even an example of that (using a type parameter) in section
14.1p12:
template<class T, T*p, class U=T> class Z {/*...*/}
According to 5.19p1, non-type template parameters of integer type
qualify to serve as constants; therefore the compiler's wrong in the
first case. However, according to that same section, only const objects
of integer type count as constant expressions; therefore the compiler's
right in the second case.
---
[ 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.research.att.com/~austern/csc/faq.html ]
Author: jerry_jeremiah@yahoo.com (=?iso-8859-1?q?William=20Jeremiah?=)
Date: Mon, 23 Jul 2001 19:05:05 GMT Raw View
I have a question. My compiler:
/opt/aCC/bin/aCC -V
aCC: HP ANSI C++ B3910B A.03.13
Produces these error messages:
template <int X, int Y = 1, int Z = X> struct Root;
Error 206: "x.cpp", line 2
Cannot cast from void expression.
template <int X, int Y = 1, int Z = X> struct Root;
^^^^^
int A = 1;
template <int X, int Y = 1, int Z = A> struct Root;
Error 519: "x.cpp", line 4
Non-type template arguments must be constant
expressions, addresses of objects or functions
with external linkage, or static class members.
template <int X, int Y = 1, int Z = A> struct Root;
^^^^^
These error messages do not seem to be valid
to me. According to the standard (actually
I am working off the Dec96 Draft at
http://www.cygnus.com/misc/wp/dec96pub
mostly because I can't afford the real thing)
a default template argument can be a value (14.1.8)
and if you follow the grammar from template
parameter (14.1.1) down to unqualified-id (5.1.6)
apparently any identifier can be used as a default
parameter.
My question is, since I don't have the actual
Standard, are these two constructs actually
valid C++?
Thanks in advance.
Jerry Jeremiah
jerry_jeremiah@yahoo.com
Bits from the Dec96 Draft showing how
I came to my conlusion:
14.1.8
A default template-argument is a type,
value, or template specified after = in a
template-parameter. A default template-argument
may be specified for both a type and non-type
template-parameter. A default template-argument
may be specified in a class template declaration
or a class template definition. A default
template-argument shall not be specified in
a function template declaration or a function
template definition. The set of default
template-arguments available for use with a
template in a translation unit shall be provided
by the first declaration of the template in that
translation unit.
14.1.1
template-parameter:
type-parameter
parameter-declaration
8.3.5.1
parameter-declaration:
decl-specifier-seq declarator
decl-specifier-seq declarator =
assignment-expression
decl-specifier-seq abstract-declaratoropt
decl-specifier-seq abstract-declaratoropt =
assignment-expression
5.17.1
assignment-expression:
conditional-expression
logical-or-expression assignment-operator
assignment-expression
throw-expression
5.16.1
conditional-expression:
logical-or-expression
logical-or-expression ? expression :
assignment-expression
5.15.1
logical-or-expression:
logical-and-expression
logical-or-expression || logical-and-expression
5.14.1
logical-and-expression:
inclusive-or-expression
logical-and-expression && inclusive-or-expression
5.13.1
inclusive-or-expression:
exclusive-or-expression
inclusive-or-expression | exclusive-or-expression
5.12.1
exclusive-or-expression:
and-expression
exclusive-or-expression ^ and-expression
5.11.1
and-expression:
equality-expression
and-expression & equality-expression
5.10.1
equality-expression:
relational-expression
equality-expression == relational-expression
equality-expression != relational-expression
5.8.7
relational-expression:
shift-expression
relational-expression < shift-expression
relational-expression > shift-expression
relational-expression <= shift-expression
relational-expression >= shift-expression
5.8.1
shift-expression:
additive-expression
shift-expression << additive-expression
shift-expression >> additive-expression
5.7.1
additive-expression:
multiplicative-expression
additive-expression + multiplicative-expression
additive-expression - multiplicative-expression
5.6.1
multiplicative-expression:
pm-expression
multiplicative-expression * pm-expression
multiplicative-expression / pm-expression
multiplicative-expression % pm-expression
5.5.1
pm-expression:
cast-expression
pm-expression .* cast-expression
pm-expression ->* cast-expression
5.4.2
cast-expression:
unary-expression
( type-id ) cast-expression
5.3.1
unary-expression:
postfix-expression
++ cast-expression
-- cast-expression
unary-operator cast-expression
sizeof unary-expression
sizeof ( type-id )
new-expression
delete-expression
5.2.1
postfix-expression:
primary-expression
postfix-expression [ expression ]
postfix-expression ( expression-listopt )
simple-type-specifier ( expression-listopt )
postfix-expression . templateopt ::opt
id-expression
postfix-expression -> templateopt ::opt
id-expression
postfix-expression . pseudo-destructor-name
postfix-expression -> pseudo-destructor-name
postfix-expression ++
postfix-expression --
dynamic_cast < type-id > ( expression )
static_cast < type-id > ( expression )
reinterpret_cast < type-id > ( expression )
const_cast < type-id > ( expression )
typeid ( expression )
typeid ( type-id )
5.1.1
primary-expression:
literal
this
:: identifier
:: operator-function-id
:: qualified-id
( expression )
id-expression
id-expression:
unqualified-id
qualified-id
5.1.6
id-expression:
unqualified-id
qualified-id
unqualified-id:
identifier
operator-function-id
conversion-function-id
~ class-name
template-id
--
Posted from web13801.mail.yahoo.com [216.136.175.11]
via Mailgate.ORG Server - http://www.Mailgate.ORG
---
[ 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.research.att.com/~austern/csc/faq.html ]