 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
sergiozin@gmail.com Guest
|
Posted: Tue Sep 27, 2005 11:21 am Post subject: Compiling error |
|
|
Hi, here goes a very simple code, to me looks like everything is right,
but I see an error compiling with VC6..., does anyone has a clue for
what's wrong?
Thanks
template<int *INT_POINTER>
class ClassA
{
public:
static void P(){}
};
int a;
int x[3];
int main()
{
/*OK*/
ClassA<&a>: ();
/*error C2964: invalid expression as template parameter*/
ClassA<&x[0]>: ();
return 0;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gerhard Menzl Guest
|
Posted: Tue Sep 27, 2005 3:36 pm Post subject: Re: Compiling error |
|
|
[email]sergiozin (AT) gmail (DOT) com[/email] wrote:
| Quote: | Hi, here goes a very simple code, to me looks like everything is
right, but I see an error compiling with VC6..., does anyone has a
clue for what's wrong?
Thanks
template<int *INT_POINTER
class ClassA
{
public:
static void P(){}
};
int a;
int x[3];
int main()
{
/*OK*/
ClassA<&a>: ();
/*error C2964: invalid expression as template parameter*/
ClassA<&x[0]>: ();
return 0;
}
|
Addresses of array elements are not valid template arguments according
of 14.3.2 of the C++ Standard.
--
Gerhard Menzl
#dogma int main ()
Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler Guest
|
Posted: Tue Sep 27, 2005 4:45 pm Post subject: Re: Compiling error |
|
|
[email]sergiozin (AT) gmail (DOT) com[/email] wrote:
| Quote: | Hi, here goes a very simple code, to me looks like everything is right,
but I see an error compiling with VC6..., does anyone has a clue for
what's wrong?
Thanks
template<int *INT_POINTER
class ClassA
{
public:
static void P(){}
};
int a;
int x[3];
int main()
{
/*OK*/
ClassA<&a>: ();
/*error C2964: invalid expression as template parameter*/
ClassA<&x[0]>: ();
return 0;
}
|
This is no problem with your compiler, but a language restriction,
because ClassA<&x[0]>: () does not fulfill the requirements described
in 14.3.2/p. 1. 14.3.2/p. 3 says:
"[Note: Addresses of array elements and names or addresses of non-static
class members are not acceptable
template-arguments. [Example:
template<int* p> class X { };
int a[10];
struct S { int m; static int s; } s;
X<&a[2]> x3; // error: address of array element
X<&s.m> x4; // error: address of non-static member
X<&s.s> x5; // error: &S::s must be used
X<&S::s> x6; // OK: address of static member
—end example] —end note]"
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
André Kempe Guest
|
Posted: Wed Sep 28, 2005 1:51 pm Post subject: Re: Compiling error |
|
|
Daniel Krügler schrieb:
<snip>
| Quote: | template<int *INT_POINTER
class ClassA
{
public:
static void P(){}
};
snip
"[Note: Addresses of array elements and names or addresses of non-static
class members are not acceptable
template-arguments. [Example:
template
int a[10];
struct S { int m; static int s; } s;
X<&a[2]> x3; // error: address of array element
snip |
But how about passing the whole array as ClassA<a>? Is this allowed by
the standard?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler Guest
|
Posted: Wed Sep 28, 2005 2:48 pm Post subject: Re: Compiling error |
|
|
André Kempe wrote:
| Quote: | Daniel Krügler schrieb:
snip
template<int *INT_POINTER
class ClassA
{
public:
static void P(){}
};
snip
"[Note: Addresses of array elements and names or addresses of non-static
class members are not acceptable
template-arguments. [Example:
template
int a[10];
struct S { int m; static int s; } s;
X<&a[2]> x3; // error: address of array element
snip
But how about passing the whole array as ClassA<a>? Is this allowed by
the standard?
|
The following is allowed:
template<int (&p)[10]> class X { };
int a[10];
X<a> x3;
This also:
template<int* p> class X { };
int a[10];
X<a> x3;
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
André Kempe Guest
|
Posted: Thu Sep 29, 2005 8:12 pm Post subject: Re: Compiling error |
|
|
Daniel Krügler schrieb:
<snip>
| Quote: | The following is allowed:
template<int (&p)[10]> class X { };
int a[10];
X<a> x3;
This also:
template<int* p> class X { };
int a[10];
X<a> x3;
snip |
So one could bypass the "adress of array element is not allowed"-issue
by giving the index as a seperate template parameter! so whats the point
of the standard to prohibit this?
Greetings from Dresden!
André
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Sat Oct 01, 2005 2:01 am Post subject: Re: Compiling error |
|
|
André Kempe wrote:
| Quote: | Daniel Krügler schrieb:
snip
The following is allowed:
template<int (&p)[10]> class X { };
int a[10];
X<a> x3;
This also:
template<int* p> class X { };
int a[10];
X<a> x3;
snip
So one could bypass the "adress of array element is not
allowed"-issue by giving the index as a seperate template
parameter! so whats the point of the standard to prohibit
this?
|
You don't even have to do that: X<a+3> works.
What is forbidden (if memory serves me correctly -- the machine
on which I have my copy of the standard is down again) is the
indexation operator itself. Something like:
template < int i > class X {} ;
int a[] = { 1, 2, 5, 10 } ;
X< a[ 2 ] > anX ;
is forbidden, for obvious reasons. Allowing &a[2] would require
a special case.
--
James Kanze GABI Software mailto:james.kanze (AT) free (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ 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
|
|