This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the EGCS project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: C++: templates


<zvyagin@gams.ihep.su> writes:
> Dear g++ developers!
>  
> My friend Alexander Shvorob sent me piece of code that demonstrated
> one problem with template arguments.
> 
> // File 2.c  ---------------------- CUT HERE ---------
> class Store { public: int i1,i2; } s;
> template <int *N> class Test {};
> 
> int main()
> {
>   Test<&s.i1> ok; 
>   Test<&s.i2> bad;
> }
> // -------------------------------- CUT HERE ---------
> 
> $ g++ 2.c
> 2.c: In function `int main()':
> 2.c:8: `(&s + 4)' is not a valid template argument
> 2.c:8: it must be the address of an object with external linkage
> 2.c:8: ANSI C++ forbids declaration `bad' with no type
> 
> $ g++ -v
> Reading specs from
> /usr/local/lib/gcc-lib/i686-pc-linux-gnulibc1/2.95/specs
> gcc version 2.95 19990718 (prerelease)
> 

Here's what I wrote when you posted this to gnu.g++.bugs on 1999-07-20.

See Section 1.4.32 [temp.arg.nontype]/3 for explanation of why this
code is illegal. [Addresses of array elements or addresses of non-
static class members are not acceptable as template-arguments]. Even
if it's a static member, you need to use &Store::i, not &s::i as the
argument.

However, gcc should also flag "ok" as an error, which is indeed a bug.

  
Regards,
Mumit


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]