Thank you for an interesting article. I wonder whether you could use the constexpr if statement to optionally provide to a function at compile-time some extra code to execute at run-time. Here's the motivating example:
#include <iostream>
struct empty
{
void operator()()
{
}
};
template<typename T = empty>
void
doit(T t = empty())
{
if constexpr (!std::is_same_v<T, empty>)
t();
}
void
foo()
{
std::cout << "Hello!\n";
}
int
main()
{
doit();
doit(foo);
}
I think this implementation is going to perform well, because:
if I don't pass a callable, no object of class empty will be created (will be optimized out), and so there will be no performance hit,
if I pass a callable, it will be called, without checking anything at run-time.
However, I wonder if you could share your expertise on whether this problem could be solved better, maybe without defining the empty type, or maybe replacing it with some standard type.
Thanks & best,
Irek
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi Bartek,
Thank you for an interesting article. I wonder whether you could use the constexpr if statement to optionally provide to a function at compile-time some extra code to execute at run-time. Here's the motivating example:
I think this implementation is going to perform well, because:
if I don't pass a callable, no object of class
empty
will be created (will be optimized out), and so there will be no performance hit,if I pass a callable, it will be called, without checking anything at run-time.
However, I wonder if you could share your expertise on whether this problem could be solved better, maybe without defining the
empty
type, or maybe replacing it with some standard type.Thanks & best,
Irek