On Wednesday, 27 January 2016 03:13:15 UTC+2, Alf P. Steinbach wrote:
> Following a suggestion from David Brown I've looked at how to make my
> simple `is_ascii` function `constexpr`, for C++11.
>
> At first I coded it as simple tail-recursive, but this had the problem
> that without optimization it could blow the machine stack when it was
> applied to a long string. So I now changed it to divide-and-conquer
> recursive, buying safety at the cost of potential inefficiency in the
> case where the arguments don't allow compile time evaluation. I gave
> this function a new name, `constexpr_is_ascii`, so that one can choose:
> efficient compile time evaluation when one knows the arguments allow
> that, or reasonably efficient checking for data known only at run time.
>
> Is there any way to /automate/ that choice?
Following example may be of use:
#include <iostream>
#include <type_traits>
template<typename T>
constexpr typename std::remove_reference<T>::type makeprval(T && t)
{
return t;
}
#define isprvalconstexpr(e) noexcept(makeprval(e))
int main(int argc, char *argv[])
{
int a = 1;
const int b = 2;
constexpr int c = 3;
const int d = argc;
std::cout << isprvalconstexpr(a) << std::endl ;
std::cout << isprvalconstexpr(b) << std::endl ;
std::cout << isprvalconstexpr(c) << std::endl ;
std::cout << isprvalconstexpr(d) << std::endl ;
}
It outputs:
0
1
1
0
That can be perhaps used to select the algorithm? I think I took it
from SO answer of Johannes Schaub and there was something about
limitations but don't remember what question it was.