Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: String lite...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 17 of 23 Topic 9495 of 9775
Post > Topic >>

Re: String literal as template parameter?

by Alberto Ganesh Barbati <AlbertoBarbati@[EMAIL PROTECTED] > Apr 18, 2008 at 05:09 AM

Piotr Rak ha scritto:
> 
> I have made hackish implementation of this, which includes
> Alberto Ganesh Barbati extension to proposal, for gcc compiler.
> It still needs some `love'. Maybe soon i will find real time to work
> on that.

That is great! It's really a good thing what you've done.

> It seems that my orginal proposal is slightly harder to implement (in
> gcc), one would have to check just if there is parameter pack in
> template parameter list too.

That's what I was afraid about, when I wrote about "advantages and
disadvantages" in my previous post.

> 
> Following example shows status of this patch (and it Works For Me TM):
> 
> #include <iostream>
> 
> template <char... Chars_>
> struct Foo {
>     static const char chars[sizeof...(Chars_)+1];
> };
> 
> template <char...Chars_>
> const char
> Foo<Chars_...>::chars[sizeof...(Chars_)+1] = {Chars_...};
> 
> template <char B_, char A_, char R_, char Term_>
> struct Bar
> {
>     static const char chars[4];
> };
> 
> template <char B_, char A_, char R_, char Term_>
> const char
> Bar<B_, A_, R_, Term_>::chars[4] = {B_, A_, R_, Term_};
> int main()
> {
>     std::cout << Foo<"foo", '\0'>::chars << std::endl;
>     std::cout << Bar<"bar", '\0'>::chars << std::endl;
> }
> 
> Compile with -std=c++0x -fstring-template-arguments

Very nice! Very. I like the idea of being able to concatenate string and
character literals in a template argument list.

> 
> The know deficiencies are:
> - doesn't handle wide literals (should be easy),
>   and unicode literals (not in gcc yet)

I guess the simplest thing is to replace the literal with a sequence of
char, char16_t, char32_t or wchar_t according to the string literal and
let the usual promotion and conversion machinery to happen. Were you
think of something different?

> - I should re****t error in case of "" or L""

Do we need to diagnose an error in this situation? Would expanding to an
empty sequence be impossible or unreasonable?

> - definitly needs testing (DejaGNU here I come!)

If you need help, please contact me privately.

> - and documentation...

If you need help, please contact me privately.

> But overall, it should be enough to see how it feels.
> 
> I am not sure if default template arguments should be sup****ted ie:
> 
> template <char... Chars_ = "Oink!">
> struct Foo;
> 
> Comments welcome :)

Ah! Good point. Currently default template-arguments cannot be specified
for parameter packs. Although the feature is very appealing, I would
leave it at that. Unless we make that a very special case, we would
actually be forced to provide a meaning for the "equivalent" syntax:

   template <char... Chars_ = 'O','i','n','k','!'>
   struct Foo;

and this would open a Pandora's box, IMHO.

So I guess we should provide wording to disallow literals in default
template-arguments, where replacement doesn't make sense.

> 
> Note that, this was first time i've seen gcc sources...

That means gcc sources are very well written or that you are very
skilled or both ;)

> For brave people, patch against gcc trunk, revision 134411
> http://doppler.no-ip.org/~prak/fstring-template-arguments.patch

Thanks! Now it's time to prepare a formal proposal. Let me help you with
that.

Ganesh

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




 23 Posts in Topic:
String literal as template parameter?
Piotr Rak <piotr.rak@[  2008-04-12 12:09:19 
Re: String literal as template parameter?
Looney <hardy_melbourn  2008-04-13 12:45:53 
Re: String literal as template parameter?
Mathias Gaunard <loufo  2008-04-13 18:21:48 
Re: String literal as template parameter?
Brendan <catphive@[EMA  2008-04-13 18:21:13 
Re: String literal as template parameter?
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-13 18:23:27 
Re: String literal as template parameter?
Sean Hunt <rideau3@[EM  2008-04-14 10:43:12 
Re: String literal as template parameter?
mark.zaytsev@[EMAIL PROTE  2008-04-14 13:13:15 
Re: String literal as template parameter?
Mathias Gaunard <loufo  2008-04-14 21:25:44 
Re: String literal as template parameter?
Alberto Ganesh Barbati &l  2008-04-14 21:24:25 
Re: String literal as template parameter?
Alberto Ganesh Barbati &l  2008-04-14 21:24:01 
Re: String literal as template parameter?
Alberto Ganesh Barbati &l  2008-04-14 21:24:40 
Re: String literal as template parameter?
Piotr Rak <piotr.rak@[  2008-04-15 12:43:13 
Re: String literal as template parameter?
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-15 23:19:28 
Re: String literal as template parameter?
David Peklak <dpeklak@  2008-04-15 23:22:35 
Re: String literal as template parameter?
Alberto Ganesh Barbati &l  2008-04-16 11:43:13 
Re: String literal as template parameter?
Piotr Rak <piotr.rak@[  2008-04-17 19:35:09 
Re: String literal as template parameter?
Alberto Ganesh Barbati &l  2008-04-18 05:09:08 
Re: String literal as template parameter?
Sean Hunt <rideau3@[EM  2008-04-18 06:13:18 
Re: String literal as template parameter?
Bart van Ingen Schenau &l  2008-04-18 17:19:53 
Re: String literal as template parameter?
gpderetta <gpderetta@[  2008-04-18 17:20:48 
Re: String literal as template parameter?
Piotr Rak <piotr.rak@[  2008-04-19 01:39:07 
Re: String literal as template parameter?
Piotr Rak <piotr.rak@[  2008-04-19 02:01:57 
Re: String literal as template parameter?
Piotr Rak <piotr.rak@[  2008-04-19 02:02:03 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Jul 8 23:35:21 CDT 2008.