# Compile time constant string manipulation?

## Recommended Posts

I've been searching on google for a while and can't find any information so I have to ask: Is it possible to manipulate constant strings using template metaprogramming, and if so, how? Every time I try, I can't seem to get from __FUNCTION__ to template<char A>. For example:
template<char A>
char ToChange()
{
return A+1;
}
template<int N>
void MyArr(const char (&arr)[N])
{
ToChange<arr[0]>;//error C2975: 'A' : invalid template argument for 'ToChange', expected compile-time constant expression
}
void func()
{
MyArr(__FUNCTION__);
}


Okay, I can see that maybe I couls pass a non-compile-time constant to MyArr. But if I do:
//ToChange is same as above
template<const char* arr>
void MyArr()
{
ToChange<arr[0]>;
}
void func()
{
MyArr<__FUNCTION__>();//error C2762: 'MyArr' : invalid expression as a template argument for 'arr'
}


It doesn't work, and I read through google that one needs to instantiate and name the string literal first, so I tried:
void func()
{
const char me[]=__FUNCTION__;
MyArr<me>();//error C2971: 'MyArr' : template parameter 'arr' : 'me' : a local variable cannot be used as a non-type argument
}


Does anyone have any ideas? Is it even possible to take a constant like __FUNCTION__ or __FUNCSIG__ and manipulate it at compile time? (I already manipulate it at run time, it just seems messed up to manipulate a compile time constant at runtime when it will always be the same)

##### Share on other sites
Telastyn    3777
#include <iostream>using   namespace       std;void    func(){static  const char *funcname=__FUNCTION__;cout << "I am: " << funcname << "\n";}int     main(){func();}

> ./testI am: func>

What beyond this sort of thing are you trying to do?

##### Share on other sites
I'm taking __FUNCSIG__, for example:
void __thiscall OuterNS::InnerNS::MyClass::MyFunction(Param&, Options*);
and turning it into xml, for example:
<function isMember=true>  <retval>void</retval>  <namespace>OuterNS::InnerNS</namespace>  <class>MyClass</class>  <name>MyFunction</name>  <params>    <p>Param&</p>    <p>Options*</p>  </params></function>

I already do this at runtime, but since __FUNCSIG__ is what's being passed in I want to do this at compile time instead of every time my log system hits a log request.

Of course, since I'm not even sure strings can be manipulated at compile time, I'm starting with something smaller, but this is my primary motivation. Simply put, once I figure out how to access strings in template metaprogramming, I think I can do the rest of it.

[Edited by - Erzengeldeslichtes on November 24, 2005 9:07:01 PM]

##### Share on other sites
Zahlman    1682
static const char* myDoc = generateXML(__FUNCSIG__);

If I'm thinking straight, will be evaluated just once, the first time the function is called.

##### Share on other sites
me22    212
You cannot have pointers as value template parameters nor floating-point numbers.

You /might/ be able to do some preprocessor hacking to get it to turn "string" into 's', 't', 'r', 'i', 'n', 'g', '\0' and then use some fancy Template Meta-Programming stuff to do your string manipulation, but I /really/ don't think it'd be worth the effort.

##### Share on other sites
Telastyn    3777
Indeed. Either way, I don't think you can actually run file routines to dump the XML out during compilation.

##### Share on other sites
Quote:
 Original post by Zahlmanstatic const char* myDoc = generateXML(__FUNCSIG__);If I'm thinking straight, will be evaluated just once, the first time the function is called.

Yeah, this was my way of doing it already. It still seems wrong, but I guess without a proper metaprogramming extension it's not going to happen.

Quote:
 Original post by me22You /might/ be able to do some preprocessor hacking to get it to turn "string" into 's', 't', 'r', 'i', 'n', 'g', '\0' and then use some fancy Template Meta-Programming stuff to do your string manipulation, but I /really/ don't think it'd be worth the effort.

Just out of curiosity, do you happen to know a preprocessor hack that would do that?

##### Share on other sites
Zahlman    1682
Quote:
 Original post by TelastynIndeed. Either way, I don't think you can actually run file routines to dump the XML out during compilation.

Indeed.

Why not write a separate script (or program, but for this kind of text parsing that's probably a bad tool for the job) that accepts your source files as input and outputs the XML files? It will need a rudimentary concept of the language (probably just counting open/close braces and looking for stuff at top scope that encounters an open brace before it does a semicolon), but that's probably much easier than whatever else might be tried here.

Better yet, try using someone else's tools for this sort of thing, like doxygen?