using ## in define

on the 'if' line, i get error : Undefined symbol 'si'
but i want it to use the value of 'i' not the variable name


1
2
3
4
5
6
7
8
9
10
11
12
13
typedef AnsiString aStr;

bool SelectCase(aStr var1, int n, aStr s1, aStr s2, aStr s3, aStr s4, aStr s5, aStr s6, aStr s7, aStr s8, aStr s9, aStr s10, aStr s11, aStr s12, aStr s13, aStr s14, aStr s15, aStr s16, aStr s17, aStr s18, aStr s19, aStr s20)
{
	#define VAR(n) s##n

	for (UINT i = 0; i < n; i++)
	{
		if (var1 == VAR(i)) return true;
	}

	return false;
}
Last edited on
Sorry, token pasting doesn't work that way. Token pasting is done in the pre-processing phase of compilation. The compiler does not know the value of i at compile time.
Might I suggest instead of aStr s1 to aStr s20 you use an array aStr s[20].

That will involve rewriting some of the other code - hopefully making it simpler.
Then your function would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
typedef AnsiString aStr;

bool SelectCase(aStr var1, int n, aStr s[20])
{

    for (UINT i = 0; i < n; i++)
    {
        if (var1 == s[i]) return true;
    }

    return false;
}
here is the rest of the code
1
2
3
4
5
6
7
8
#define SELECTCASE(sMainStr)    { AnsiString sTmpStr = sMainStr;          if (0) {
#define CASEANY(N, ...)         } else if (SelectCase(sTmpStr, N, __VA_ARGS__))  {
#define ELSE                    } else {
#define ENDSELECT               }}

#define GET_NUM_ARGS_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, N, ...) N
#define GET_NUM_ARGS(...) GET_NUM_ARGS_IMPL(__VA_ARGS__, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
#define CASE(...) CASEANY(GET_NUM_ARGS(__VA_ARGS__), __VA_ARGS__) 


i call it like that
1
2
3
4
5
6
7
8
	SELECTCASE(var1)
	CASE("TRUE", "VRAI", "YES", "OUI", "1", "-1")
		return true;
	CASE("FALSE", "FAUX", "NO", "NON", "0")
		return false;
	ELSE
		return zDefault;
	ENDSELECT


it won't work with an array, i get the error : Cannot convert 'char const[5]' to 'AnsiString *'

is there a way to convert any number of parameters to array with a macro ?



EDIT :

i solved it with this
1
2
3
4
5
6
	aStr * x[20] = {&s1, &s2, &s3, &s4, &s5, &s6, &s7, &s8, &s9, &s10, &s11, &s12, &s13, &s14, &s15, &s16, &s17, &s18, &s19, &s20};

	for (int i = 0; i < n; i++)
	{
		if (var1 == *(x[i])) return true;
	}

Last edited on
Topic archived. No new replies allowed.