Re: String literal as template parameter?
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 report 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 supported 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! ]