C++ Core Guidelines Per.1: Don't optimize without reason
Reason:
If there is no need for optimization, the main result of the effort will be more errors and higher maintenance costs.
As for me, I will use
std::cout<<is_sqrt ? std::sqrt(i) : i*i;
I feel like this might be slightly faster and doesn't introduce too much complexity.