By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,120 Members | 1,257 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,120 IT Pros & Developers. It's quick & easy.

char[] as template argument

P: n/a
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 (mr************@hotmail.com)

Jul 25 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
mrstephengross wrote:
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 (mr************@hotmail.com)


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

Jul 25 '05 #2

P: n/a
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

Jul 25 '05 #3

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

--Steve

Jul 25 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.