C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

char* as template parameter

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Alexander Kamotsky
Guest





PostPosted: Fri Nov 19, 2004 3:31 pm    Post subject: char* as template parameter Reply with quote



Hello all.

My Borland C++ Builder 5 has betrayed me once again. =)

Today I've checked this code under MinGW g++ 3.4.2 and VC++.Net and it
did not compile:

template <char *E> struct A {};

char c1 = 'a';
char c2[] = "asd"; // (1)
char* c3 = "qwe"; // (2)

A<&c1> a1; // ok
A<c2> a2; // ok!
A<c3> a3; // an error? =

Well, I even tried to rewrite (2):

const char* const c3 = "qwe"; // (3)

The same result. Compilers says something like "template parameter
should be constant expression"... But I can't catch the logic of the
acception of (1) and the rejection of (3). Would somebody explain me
this issue? =) Thanks.

PS: Borland compiler is accepting (2) but not (3). %-

--
Best regards,
Alexander mailto:alexandroid (AT) p5com (DOT) com


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Sat Nov 20, 2004 10:35 am    Post subject: Re: char* as template parameter Reply with quote



Alexander Kamotsky wrote:
Quote:
My Borland C++ Builder 5 has betrayed me once again. =)

Today I've checked this code under MinGW g++ 3.4.2 and VC++.Net and it
did not compile:

template <char *E> struct A {};

char c1 = 'a';
char c2[] = "asd"; // (1)
char* c3 = "qwe"; // (2)

A<&c1> a1; // ok
A<c2> a2; // ok!
A<c3> a3; // an error? =

Well, I even tried to rewrite (2):

const char* const c3 = "qwe"; // (3)

The same result. Compilers says something like "template parameter
should be constant expression"... But I can't catch the logic of the
acception of (1) and the rejection of (3). Would somebody explain me
this issue? =) Thanks.

'c3' is a pointer that can change its value. '&c1' can't. Neither can
'c2'. Another problem: when you declare 'c3' a const pointer to const
char, it gets _internal_ linkage. A non-type template argument that is
a pointer to an object, has to have external linkage:

extern char const* c3 = "qwe";

A<c3> a3; // should be fine.

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Alex Vinokur
Guest





PostPosted: Sat Nov 20, 2004 6:21 pm    Post subject: Re: char* as template parameter Reply with quote




"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote

Quote:
Alexander Kamotsky wrote:
My Borland C++ Builder 5 has betrayed me once again. =)

Today I've checked this code under MinGW g++ 3.4.2 and VC++.Net and it
did not compile:

template <char *E> struct A {};

char c1 = 'a';
char c2[] = "asd"; // (1)
char* c3 = "qwe"; // (2)

A<&c1> a1; // ok
A<c2> a2; // ok!
A<c3> a3; // an error? =

Well, I even tried to rewrite (2):

const char* const c3 = "qwe"; // (3)

The same result. Compilers says something like "template parameter
should be constant expression"... But I can't catch the logic of the
acception of (1) and the rejection of (3). Would somebody explain me
this issue? =) Thanks.

'c3' is a pointer that can change its value. '&c1' can't. Neither can
'c2'. Another problem: when you declare 'c3' a const pointer to const
char, it gets _internal_ linkage. A non-type template argument that is
a pointer to an object, has to have external linkage:

extern char const* c3 = "qwe";

A<c3> a3; // should be fine.
[snip]


GNU g++ 3.4 doesn't accept that.

------ foo.cpp ------
template <char const* T>
struct Foo {};

extern char const* ch = "qwe";

int main ()
{
Foo<ch> foo; // Line#8
return 0;
}
---------------------

------ Compilation ------

$ gpp --version
gpp.exe (GCC) 3.4.1
[---omitted---]

$ gpp foo.cpp
foo.cpp: In function `int main()':
foo.cpp:8: error: `ch' is not a valid template argument
foo.cpp:8: error: it must be the address of an object with external linkage
foo.cpp:8: error: invalid type in declaration before ';' token

-------------------------

Digital Mars C++ 8.40.2 doesn't accept that too.


Borland C++ 5.5.1 and Microsoft C++ 13.00.9466 accept foo1.cpp
------ foo1.cpp ------
template <char const* T>
struct Foo {};

char const* ch = "qwe"; // no extern !!!!

int main ()
{
Foo<ch> foo;
return 0;
}

----------------------

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Thomas Mang
Guest





PostPosted: Sun Nov 21, 2004 5:02 am    Post subject: Re: char* as template parameter Reply with quote


"Alex Vinokur" <alexvn (AT) big-foot (DOT) com> schrieb im Newsbeitrag
news:308q42F2qer5hU1 (AT) uni-berlin (DOT) de...
Quote:

"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:8rond.11173$Ae.979 (AT) newsread1 (DOT) dllstx09.us.to.verio.net...
Alexander Kamotsky wrote:
My Borland C++ Builder 5 has betrayed me once again. =)

Today I've checked this code under MinGW g++ 3.4.2 and VC++.Net and
it
did not compile:

template <char *E> struct A {};

char c1 = 'a';
char c2[] = "asd"; // (1)
char* c3 = "qwe"; // (2)

A<&c1> a1; // ok
A<c2> a2; // ok!
A<c3> a3; // an error? =

Well, I even tried to rewrite (2):

const char* const c3 = "qwe"; // (3)

The same result. Compilers says something like "template parameter
should be constant expression"... But I can't catch the logic of the
acception of (1) and the rejection of (3). Would somebody explain me
this issue? =) Thanks.

'c3' is a pointer that can change its value. '&c1' can't. Neither can
'c2'. Another problem: when you declare 'c3' a const pointer to const
char, it gets _internal_ linkage. A non-type template argument that is
a pointer to an object, has to have external linkage:

extern char const* c3 = "qwe";

A<c3> a3; // should be fine.
[snip]

GNU g++ 3.4 doesn't accept that.


And is conforming to the Standard regarding this.

Using an extern const char[] should do the trick:

template <char const* T>
struct Foo {};

extern char const ch[] = "qwe";

int main ()
{
Foo<ch> foo;
return 0;
}


Don't ask me about details. I remember this trick from "C++ Template: The
complete guide".


Thomas



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Victor Bazarov
Guest





PostPosted: Sun Nov 21, 2004 6:54 am    Post subject: Re: char* as template parameter Reply with quote

"Alex Vinokur" <alexvn (AT) big-foot (DOT) com> wrote...
Quote:

"Victor Bazarov" <v.Abazarov (AT) comAcast (DOT) net> wrote in message
news:8rond.11173$Ae.979 (AT) newsread1 (DOT) dllstx09.us.to.verio.net...
Alexander Kamotsky wrote:
My Borland C++ Builder 5 has betrayed me once again. =)

Today I've checked this code under MinGW g++ 3.4.2 and VC++.Net and it
did not compile:

template <char *E> struct A {};

char c1 = 'a';
char c2[] = "asd"; // (1)
char* c3 = "qwe"; // (2)

A<&c1> a1; // ok
A<c2> a2; // ok!
A<c3> a3; // an error? =

Well, I even tried to rewrite (2):

const char* const c3 = "qwe"; // (3)

The same result. Compilers says something like "template parameter
should be constant expression"... But I can't catch the logic of the
acception of (1) and the rejection of (3). Would somebody explain me
this issue? =) Thanks.

'c3' is a pointer that can change its value. '&c1' can't. Neither can
'c2'. Another problem: when you declare 'c3' a const pointer to const
char, it gets _internal_ linkage. A non-type template argument that is
a pointer to an object, has to have external linkage:

extern char const* c3 = "qwe";

A<c3> a3; // should be fine.
[snip]

GNU g++ 3.4 doesn't accept that.
[..]

Ought to be

extern char const c3[] = "qwe";

V


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.