char[] as template argument

M

mrstephengross

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 ([email protected])
 
J

Josh Mcfarlane

mrstephengross said:
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 ([email protected])

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

mrstephengross

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
 
M

mrstephengross

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

--Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top