Reputation: 2321
I got a problem on where I have something like this:
#define A_BODY printf("b is %s and c is %d, typeless! :3\n", b, c);
#define B_BODY return "test";
#define C_BODY return 42;
I need to make a code that would, for example, call a(b(), c()), with their respective types.
On C++14 I can easely do this:
template<typename B, typename C> auto a(B &&b, C &&c) {
A_BODY
};
auto b() {
B_BODY
};
auto c() {
C_BODY
};
int main() {
auto _b = b();
auto _c = c();
auto _a = a(_b, _c);
return 0;
};
Achieving the desired result... is there any way to get the same result on C++11? :'(
Also, they can call each other recursively, so a simple ordering here wouldn't help either.
I will try to explain better my situation.
I got an input file like this, for example:
a is b c {
printf("b is %s and c is %d\n", b, c);
if(c > 42)
printf("c is bigger than the truth!\n");
return strlen(b) + c;
};
b is {
return "test";
};
c is {
return 42;
};
And I need to generate a C++ code that can properly call them. I'm trying to infer the return types without needing to define them on the input file.
From that, I can get the *_BODY
specifications, the number of parameters, and the order of calls, but I don't know which type the parameters will be. So I'd need an templated lambda, for example, to do lazy evaluation of the function body.
I can do this with C++14 on GCC and CLang, but this is a commercial project and I need to support Visual Studio 2012 as well. I'm trying to find a workaround, if there is any. :(
Upvotes: 0
Views: 587
Reputation: 7637
Could do it like this:
#define A_EXPR printf("b is %s and c is %d, typeless! :3\n", b, c)
#define B_EXPR "test"
#define C_EXPR 42
template<typename B, typename C> auto a(B &&b, C &&c)
-> decltype(A_EXPR) { return A_EXPR; }
auto b() -> decltype(B_EXPR) { return B_EXPR; }
auto c() -> decltype(C_EXPR) { return C_EXPR; }
int main() {
auto _b = b();
auto _c = c();
auto _a = a(_b, _c);
return 0;
};
Works fine in clang. On the other hand, gcc (4.8.1) complains that
sorry, unimplemented: string literal in function template signature
for function a
, but you don't really need a result for that; it could be just void
.
Upvotes: 1