On Friday, 24 August 2018 10:29:23 UTC+3, David Brown wrote:
> Can you write something similar in C++, so that you have:
>
> template<typename T> constexpr const bool isConstantExpression(T x);
>
>
> I can't think of a way to do it myself, but maybe that's just my lack of
> imagination.
That perhaps works:
#include <iostream>
#include <type_traits>
template<typename T>
constexpr typename std::remove_reference<T>::type makeprval(T && t)
{
return t;
}
int main(int argc, char *argv[])
{
int a = 1;
const int b = 2;
constexpr int c = 3;
const int d = argc;
// b and c are compile time constants but a and d are not.
std::cout << std::boolalpha;
std::cout << "a " << noexcept(makeprval(a)) << "\n";
std::cout << "b " << noexcept(makeprval(b)) << "\n";
std::cout << "c " << noexcept(makeprval(c)) << "\n";
std::cout << "d " << noexcept(makeprval(d)) << "\n";
}