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 16 of 23 Topic 9495 of 9831
Post > Topic >>

Re: String literal as template parameter?

by Piotr Rak <piotr.rak@[EMAIL PROTECTED] > Apr 17, 2008 at 07:35 PM

On Apr 16, 7:19 am, Daniel Krügler <daniel.krueg...@[EMAIL PROTECTED]
>
wrote:
> On 15 Apr., 05:24, Alberto Ganesh Barbati <AlbertoBarb...@[EMAIL PROTECTED]
>
> wrote:
>
> > I also don't find it compelling to require that the string literal
> > appears as the last argument. Although I understand the intent, I
don't
> > think the requirement provides a true simplification for the
> > implementor, yet it may limit some possibly valid uses. Moreover, if
we
> > find the proper wording, we might even allow string literals to match
> > non-variadic templates. For example, consider a  library component
> > implementing a FourCC (http://en.wikipedia.org/wiki/Fourcc):
>
> >    template <char A, char B, char C, char D> class FourCC
>
> >    FourCC<"RIFF"> riff; // ok: maps to FourCC<'R', 'I', 'F', 'F'>
>
> > isn't that neat? Of course, FourCC<"RIF"> and FourCC<"RIFFF"> would be
> > ill-formed. The key is to simply replace the literal with the sequence
> > of single characters and then apply the "normal" template machinery.
>
> I think that this extension proposal goes to far
> and that there exist an alternative given concepts
> to realize the same thing.
>
> The problem I see is, that this approach might seem
> natural for human tolerant pattern matching, but it
> is not so well-situated for the compiler. It seems
> natural for human beings, because it bases on some
> form of convention: "Just assume that all template
> parameters are non-type parameters and each one has
> the same type and this type must be a character type".
>

I think that is not a problem, am I missing something obvious?
So far i was not able to construct example when proposed aproach would
be overly expensive. It tends to be rather simple tree subsitution.
Compiler logic needed for that is already present
to handle variadic argument pack expansion.

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.
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.

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

The know deficiencies are:
- doesn't handle wide literals (should be easy),
  and unicode literals (not in gcc yet)
- I should re****t error in case of "" or L""
- definitly needs testing (DejaGNU here I come!)
- and do***entation...

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 :)

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

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

Piotr Rak


-- 
      [ 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 Fri Jul 25 21:58:30 CDT 2008.