Failing compilation
by evaluating
a concept
Braden Ganetsky
CppNorth 2024 Lightning Talk
How I wish I could
constrain my
function template
Braden Ganetsky
CppNorth 2024 Lightning Talk
## This talk is about * Function templates * Putting conditions on function templates
Conditions on a function template
static_assert
Constraints
## Benefits of `static_assert` * User-specified diagnostic message
## Benefits of constraints * Callability evaluated as a predicate * Removes certain functions from overload set `foo`
Benefits of constraints
## If we used `static_assert` * `Fooable
` instantiates `foo
` * `foo
` hits the `static_assert` * Compilation failure from evaluating a concept (#1)
## What do I want? * User-specified diagnostic message * Also removing from the overload set * Is this possible?
What do I want?
## Idea #1 * `static_assert` inside the concept
Idea #1 - example
## Idea #1 - problems * The `static_assert` is never triggered (in MSVC at least)
## Idea #2 * Factor out the lambda
Idea #2 - example
## Idea #2 - problems * Same problems as a `static_assert` used regularly * Fundamental conflict: signature versus body
## Idea #3 * Credit to Patrick Roberts * Mix concepts with `enable_if` SFINAE
Idea #3 - example
## Idea #3 - problems * Lack of user-specified message * However, compilation failure from evaluating a concept (#2)
## Idea #4 * Custom `enable_if` implementation
Idea #4 - example
Idea #4 - example
## Idea #4 - problems * Almost completely satisfies what I want, however:
Idea #4 - problems
## Idea #4 - problems * It removes `foo
` from the overload set * I *still* don't get the error message I want
## Conclusion * I like writing constraints to check callability * I also like user-specified error messages * I want to mix them together * I haven't yet figured out how * ... * If you have an idea, please talk to me later!
Thank you!
Braden Ganetsky
braden@ganets.ky
GitHub @k3DW