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

Feature Request: STATIC_REQUIRE macro #1356

Closed
parnmatt opened this issue Aug 8, 2018 · 3 comments
Closed

Feature Request: STATIC_REQUIRE macro #1356

parnmatt opened this issue Aug 8, 2018 · 3 comments

Comments

@parnmatt
Copy link
Contributor

parnmatt commented Aug 8, 2018

I've just starting using Catch2, and find it very expressive.

There are some tests that can be done at compile time; for example I am writing an adapter library and need to test if two types are implicitly convertible.

I could use REQUIRE(std::is_convertible_v<A, B>); which fits in nicely with this test framework.
However the argument is known at compile time. I've found myself doing something like:

static_assert(std::is_convertible_v<A, B>, "A should be convertible to B");
SUCCEED("A and B are convertible");

Such that the static test is logged and documented, but done at compile time.

Could there be a macro be included to wrap around that?

#define STATIC_REQUIRE(expression, message) {  \
    static_assert(expression, message);        \
    SUCCEED(message);                          \
}

That way it's automatically statically tested, and logged. It would be nice for the expression to be in the output itself; which I guess can be emulated with:

#define STATIC_REQUIRE(expression, message) {  \
    static_assert(expression, message);        \
    REQUIRE(expression);                       \
}

which would actually test twice, once at compile time and once at run time; though it should be inexpensive as its likely to have been calculated at compile-time.

@martinmoene
Copy link
Collaborator

martinmoene commented Aug 8, 2018

Perhaps you want to be able to also let STATIC_REQUIRE behave as the normal REQUIRE so that compilation may continue where it otherwise would be stopped by a static assertion failure.

Using somewhat like this here

@parnmatt
Copy link
Contributor Author

parnmatt commented Aug 9, 2018

I wasn't thinking that; however that's a fantastic idea, and simple to implement with a variable as you've shown.

That would be a great addition to this feature request.

@horenmar
Copy link
Member

I like the idea, so I am open to getting the PR in. (I am gonna post feedback on that there)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants