Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for expressions with template arguments. #5

Open
CalebAdrianXYZ opened this issue Feb 22, 2022 · 3 comments
Open

Support for expressions with template arguments. #5

CalebAdrianXYZ opened this issue Feb 22, 2022 · 3 comments
Labels
backlog enhancement New feature or request

Comments

@CalebAdrianXYZ
Copy link

Currently, the following code fails to compile:

template<int X, int Y> bool foo() { return X == Y; }

int main()
{
    foo<3,4>();
    ASSERT(foo<3,4>());
}

This code can be seen in action on compiler explorer.

This is due to foo<3,4>() being seen as two arguments when passed to a macro, specifically foo<3 and 4>(). This can be worked around with an extra set of parenthesis. Working around this in the library would be difficult, but should be possible. Decomposition would need to overload operator, to handle the error message string, and additional diagnostic variables. The top level macros would then consume a single variadic argument pack.

@jeremy-rifkin
Copy link
Owner

Hi @cra2801, I think I see what you're proposing. It'd be a cool enhancement, it will add a lot of complexity to the expression decomposer but I think it could work. I will try to play around with it when I get the chance!

@jeremy-rifkin jeremy-rifkin added enhancement New feature or request backlog labels Apr 17, 2022
@jeremy-rifkin
Copy link
Owner

Supporting this would involve further complicating an already intricate expression decomposition system and I'm concerned about possible impact on build times. Possibly the sort of thing that would make sense to optionally enable/disable, but that'd probably involve maintaining two expression decomposition systems. I'm going to mark this as a backlog enhancement for now.

@jfalcou
Copy link

jfalcou commented Jul 2, 2022

You cna draft a small macro to take any token like (SOME, STUFF) into SOME,THING. I use it in TTS.
Here is a small version:

// Remove parens around macro token if any are present 
#define TTS_REMOVE_PARENS(x)              TTS_EVAL((TTS_REMOVE_PARENS_I x), x)
#define TTS_REMOVE_PARENS_I(...)          1, 1
#define TTS_APPLY(macro, args)            TTS_APPLY_I(macro, args)
#define TTS_APPLY_I(macro, args)          macro args
#define TTS_EVAL_I(test, x)               TTS_MAYBE_STRIP_PARENS(TTS_TEST_ARITY test, x)
#define TTS_EVAL(test, x)                 TTS_EVAL_I(test, x)
#define TTS_TEST_ARITY(...)               TTS_APPLY(TTS_TEST_ARITY_I, (__VA_ARGS__, 2, 1))
#define TTS_TEST_ARITY_I(a, b, c, ...)    c
#define TTS_MAYBE_STRIP_PARENS(cond, x)   TTS_MAYBE_STRIP_PARENS_I(cond, x)
#define TTS_MAYBE_STRIP_PARENS_I(cond, x) TTS_CAT(TTS_MAYBE_STRIP_PARENS_, cond)(x)
#define TTS_MAYBE_STRIP_PARENS_1(x)       x
#define TTS_MAYBE_STRIP_PARENS_2(x)       TTS_APPLY(TTS_MAYBE_STRIP_PARENS_2_I, x)
#define TTS_MAYBE_STRIP_PARENS_2_I(...)   __VA_ARGS__

Thus you can ask people to put () in their expression and pass the macro argument through TTS_REMOVE_PARENS.
If no parens is there, the token are left untouched.

@jeremy-rifkin jeremy-rifkin added c++26 and removed c++26 labels Feb 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants