Sign in to follow this  
Erzengeldeslichtes

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 this post


Link to post
Share on other sites

#include <iostream>

using namespace std;


void func(){
static const char *funcname=__FUNCTION__;
cout << "I am: " << funcname << "\n";
}


int main(){
func();
}




> ./test
I am: func
>


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
static 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 me22
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.

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

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
Indeed. 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?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this