# C++ String Template Parameters outside C++11/14/17

## Recommended Posts

Hey,

I was fiddling arround with RegEx-es while I tried to implement them on an old machine without using any C++11/14/17 related syntactic sugar like constexpr or variadic templates but still as compile-time evaluated statement list. But without any luck as one might already get

My problem to solve is getting a convinient way to make a list of character parameters from a compile-time string literal to pass into the template based compile-time deduction process. This may look something like this

template<char a0> struct CharacterList <a0> //<<- as string???
{
public:
typedef Evaluator<a0> Eval;
};
template<char a0, char a1> struct CharacterList <a0a1> //<<- as string???
{
public:
typedef Evaluator<a0, Evaluator<a1>> Eval;
};
template<char a0, char a1, char a2> struct CharacterList <a0a1a2> //<<- as string???
{
public:
typedef Evaluator<a0, Evaluator<a1, Evaluator<a2>>> Eval;
};

int main()
{
Regex r = CharacterList<"MyRegexSyntax">::Eval;
}

I know this is anything but working without the new ISO features but was corious if someone has any workarround for that except defining strings as an array of characters

##### Share on other sites

Sounds like you want the compiler to parse components of a string, and generate code at compile-time based on that compile-time string, instead of generating code to emit data structures at runtime based on a runtime string.

That's the very purpose of constexpr, and AFAIK there is no standard C++ way to do that before C++17... and it might even be pretty tricky in C++20.

That said, here's someone working on the same thing: https://github.com/hanickadot/compile-time-regular-expressions

##### Share on other sites

I already saw this but as I wrote on-top

On 29.1.2018 at 2:39 PM, Shaarigan said:

I tried to implement them on an old machine without using any C++11/14/17

the drawback is that this (or better the compiler; thank you Microsoft) dosent support constexpr and is itself very ... bitchy .. (is that a word? )about compile time code verification. Thats why I needed to write a list of char literals instead of string even on the fact that I have a variadic macro replacement (like the one from boost) that already works and compiles oin different compilers (MSVC 2010, 2015, clang LLVM)

#ifndef LITERAL_MAX_LENGTH
#define LITERAL_MAX_LENGTH 70
#endif

struct ErrorType;

/**
A compile time expression
*/
template<char c, typename list> struct Expression
{
typedef ErrorType Result;
};
/*
template<typename list> struct Expression<'['>
{
};
*/

/**
A compile time static literal expression
*/
template<VARIADIC_PARAMS_PAIR(char c, = 0, LITERAL_MAX_LENGTH)> struct Literal;
{ \
enum { Head = (c0) }; \
typedef typename Literal<VARIADIC_PARAMS_SHIFT(c, n)> Tail; \
\
typedef typename Expression<Head, Tail>::Result Compile; \
static const char Value[n + 1]; \
}; \

IT_FOR(DECREMENT(LITERAL_MAX_LENGTH), LITERAL_DEFINITION)

#define litexpr(...) Literal<__VA_ARGS__>
#undef LITERAL_DEFINITION
#undef LITERAL_MAX_LENGTH

But is still a bit tricky to use, even when going for the macro

litexpr('[','^',' ',']','*')::Compile //works but is ugly to use
litexpr("[^ ]*")::Compile //dosent work; MSVC complains until 2013 (constexpr isnt supported) and even sometimes in 2015 that this is not a compile time constant

What I like to achieve here is to have compile-time verified/ assembled expressions that could be used for example in a regulary test regex or something like a mesh; or in general to have compile-time expression code that is generated into runtime usable parser/verifier/lambda/<insert whatever here>. It dosent need to verify anything at compile-time but simply compile the expression into executable code. I know it is hard and tricky to get this running (and sometimes a waste of time) but its just an experimental feature to speed up things

Edited by Shaarigan
Changed code example for Bregma's answer

##### Share on other sites

The mechanism they introducd into C++11 to do exactly this was the template parameter pack (AKA variadic templates).  Prior to that, you could use Alexandrescu's typelists or something similar.

It's pretty much an academic exercise:  processing a regex at compile time gives you the benefit of incredible and error-time complexity at the cost of massive code bloat.  But it's nevertheless an interesting academic exercise, so carry on.

##### Share on other sites

Thank you for the typelist suggestion, that made life a little easier for this as I could now pass Head and the Tail-List arround as separate parameters. I thought about the topic and came to the conclusion that it would be a possible goal to have a list of function pointers at the end so my regex/filter/lambda class could act like

class Regex
{
public:
inline Regex(const StaticContext<bool (IDataStream const&)>* validator) : validator(validator)
{ }

inline bool Match(IDataStream const& data)
{
StaticContext<bool (IDataStream const&)> tmp* = validator;
while(tmp && *tmp(data))
tmp++;

return !(tmp);
}

protected:
StaticContext<bool (IDataStream const&)> validator*;
}

In this case, we would check a set of function pointers for returning true each on some data and result if we reached the end (zero) of that list so we could be sure we found a match

## Create an account

Register a new account

• 9
• 47
• 11
• 17
• 11
• ### Similar Content

• Hi guys,
I am having problems with trying to perform a basic 'shift left' on a char.
char temp[1]; temp[0] = buffer[0] << 1; // buffer[0] is 0xff After this I have temp[0] writing to a file. Instead of being the expected 0x7F it is written as 0xF8.
Any guidance on what I am doing wrong would be awesome.

• Hi all
this is my first post on this forum.
First of all i want to say you that i've searched many posts on this forum about this specific argument, without success, so i write another one....
Im a beginner.
I want use GPU geometry clipmaps algorithm to visualize virtual inifinte terrains.
I already tried to use vertex texture fetch with a single sampler2D with success.

Readed many papers about the argument and all speak about the fact that EVERY level of a geometry clipmap, has its own texture. What means this exactly? i have to
upload on graphic card a sampler2DArray?
With a single sampler2D is conceptually simple. Creating a vbo and ibo on cpu (the vbo contains only the positions on X-Z plane, not the heights)
and upload on GPU the texture containing the elevations. In vertex shader i sample, for every vertex, the relative height to te uv coordinate.
But i can't imagine how can i reproduce various 2d footprint for every level of the clipmap. The only way i can imagine is follow:
Upload the finer texture on GPU (entire heightmap). Create on CPU, and for each level of clipmap, the 2D footprints of entire clipmap.
So in CPU i create all clipmap levels in terms of X-Z plane. In vertex shader sampling these values is simple using vertex texture fetch.
So, how can i to sample a sampler2DArray in vertex shader, instead of upload a sampler2D of entire clipmap?

Sorry for my VERY bad english, i hope i have been clear.

• By mangine
Hello. I am developing a civ 6 clone set in space and I have a few issues. I am using Lua for the logic and UI of the game and c++ directx 12 for the graphics. I need a way to send information between Lua and c++ occasionally and was wondering what is the best and most flexible (and hopefully fast) way to do this. Don't forget that I also need to send things from c++ back to Lua. I know of a lua extension called "LuaBridge" on github but it is a little old and I am worried that it will not work with directx 12. Has anybody done something similar and knows a good method of sending data back and forth? I am aware that Lua is used more and more in the industry and surely plenty of AAA game programmers know the answer to this. I want a good solution that will hopefully still be viable code in a couple of years...
• By owenjr
Hi there.
I'm pretty new to this and I don't know if it has been asked before, but here I go.
I'm developing a game using SFML and C++.
I would like to use the "Tiled" tool to load maps into my game but I don't actually find any tutorial or guide on how to exaclty use it (I know that I have to read an XML file and stuff). I just step into diverse projects that make all a mess.
Anyone knows where can I find good information to make my map loader by myself?