Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 25th, 2005, 02:35 PM
mrstephengross
Guest
 
Posts: n/a
Default char[] as template argument

I'm trying to find a way to parse apart a string at compile time. I'm
guessing it can't be done. But maybe someone on this group has some
ideas... Here's where I am so far: I know how to templatize a class on
a 'const char *' (see pg. 40 on Vandevoorde's "C++ Templates"). The
code looks like this:

=======================================
extern char const foo [] = "hello";
template<const char * P> class Foo { };
int main() { Foo<foo> f; return 0;
=======================================

So that much works. The next step I want to do is create a class
templatized on a single character, and then instantiate it with the
first character in the foo string. Since foo is const, I figured there
might be a way to make the following work:

=======================================
extern char const foo [] = "hello";
template<char t> class Bar { };
int main() { Bar<foo[0]> b; return 0;
=======================================

Sadly, it does not. The compiler complains that foo[0] is a non-const
argument. Is there some way to get the compiler to understand that the
contents of foo are constant?

Thanks,
--Steve (mrstephengross@hotmail.com)

  #2  
Old July 25th, 2005, 04:45 PM
Josh Mcfarlane
Guest
 
Posts: n/a
Default Re: char[] as template argument

mrstephengross wrote:[color=blue]
> I'm trying to find a way to parse apart a string at compile time. I'm
> guessing it can't be done. But maybe someone on this group has some
> ideas... Here's where I am so far: I know how to templatize a class on
> a 'const char *' (see pg. 40 on Vandevoorde's "C++ Templates"). The
> code looks like this:
>
> =======================================
> extern char const foo [] = "hello";
> template<const char * P> class Foo { };
> int main() { Foo<foo> f; return 0;
> =======================================
>
> So that much works. The next step I want to do is create a class
> templatized on a single character, and then instantiate it with the
> first character in the foo string. Since foo is const, I figured there
> might be a way to make the following work:
>
> =======================================
> extern char const foo [] = "hello";
> template<char t> class Bar { };
> int main() { Bar<foo[0]> b; return 0;
> =======================================
>
> Sadly, it does not. The compiler complains that foo[0] is a non-const
> argument. Is there some way to get the compiler to understand that the
> contents of foo are constant?
>
> Thanks,
> --Steve (mrstephengross@hotmail.com)[/color]

Have you tried template<const char t> class bar { }?

  #3  
Old July 25th, 2005, 05:15 PM
mrstephengross
Guest
 
Posts: n/a
Default Re: char[] as template argument

No, that's not the point... I want to see if its possible to extract a
single character as from a const char array at compile time, and use it
as a template argument later on. Obviously, template<const char t>
works for chars--but is it possible to use a character from a const
char [] as a template argument?

--Steve

  #4  
Old July 25th, 2005, 05:25 PM
mrstephengross
Guest
 
Posts: n/a
Default Re: char[] as template argument

Or did you mean to try "const char t" instead of "char t"? If so: yeah,
I did try that... No luck :(

--Steve

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles