Skip to content

Instantly share code, notes, and snippets.

@gatchamix
Created December 12, 2017 10:43
Show Gist options
  • Select an option

  • Save gatchamix/20467487c31bb040db5f0acf269bdff1 to your computer and use it in GitHub Desktop.

Select an option

Save gatchamix/20467487c31bb040db5f0acf269bdff1 to your computer and use it in GitHub Desktop.
Improved is_constexpr checking macro (by quicknir)
#include <utility>
#include <string>
#include <string_view>
using namespace std::literals;
//
auto constexpr checker(...) {}
#define is_constexpr(...) \
noexcept(checker(__VA_ARGS__))
//
int main()
{
// empty check
static_assert(is_constexpr());
// literal values
static_assert(is_constexpr("hello"));
static_assert(is_constexpr("hello"sv));
static_assert(!is_constexpr("hello"s));
// constexpr values
auto constexpr a = "hello";
auto constexpr b = "hello"sv;
static_assert(is_constexpr(a));
static_assert(is_constexpr(b));
// non-constexpr values
auto c = "hello";
auto d = "hello"s;
auto e = "hello"sv;
static_assert(!is_constexpr(c));
static_assert(!is_constexpr(d));
static_assert(!is_constexpr(e));
// non-capture lambda as literal (NOTE: relies on p0315)
#if 0
static_assert(is_constexpr([]{}));
static_assert(is_constexpr([]{ return "hello"; }));
static_assert(is_constexpr([]{ return "hello"sv; }));
static_assert(is_constexpr([]{ return "hello"s; }));
static_assert(is_constexpr([]{ return "hello"; }()));
static_assert(is_constexpr([]{ return "hello"sv; }()));
static_assert(is_constexpr([]{ return "hello"s; }()));
#endif
// capture lambda as literal (NOTE: relies on p0315)
#if 0
static_assert(is_constexpr([&]{}));
static_assert(is_constexpr([&]{ return "hello"; }));
static_assert(is_constexpr([&]{ return "hello"sv; }));
static_assert(is_constexpr([&]{ return "hello"s; }));
static_assert(is_constexpr([&]{ return "hello"; }()));
static_assert(is_constexpr([&]{ return "hello"sv; }()));
static_assert(is_constexpr([&]{ return "hello"s; }()));
#endif
// constexpr non-capture lambda
auto constexpr lam1 = []{};
auto constexpr lam2 = []{ return "hello"; };
auto constexpr lam3 = []{ return "hello"sv; };
auto constexpr lam4 = []{ return "hello"s; };
static_assert(is_constexpr(lam1));
static_assert(is_constexpr(lam2));
static_assert(is_constexpr(lam3));
static_assert(is_constexpr(lam4));
static_assert(is_constexpr(lam2()));
static_assert(is_constexpr(lam3()));
static_assert(!is_constexpr(lam4()));
// non-constexpr non-capture lambda
auto lam5 = []{};
auto lam6 = []{ return "hello"; };
auto lam7 = []{ return "hello"sv; };
auto lam8 = []{ return "hello"s; };
static_assert(is_constexpr(lam5));
static_assert(is_constexpr(lam6));
static_assert(is_constexpr(lam7));
static_assert(is_constexpr(lam8));
static_assert(is_constexpr(lam6()));
static_assert(is_constexpr(lam7()));
static_assert(!is_constexpr(lam8()));
// constexpr capture lambda
auto constexpr cap1 = [&]{};
auto constexpr cap2 = [&]{ return a; };
auto constexpr cap3 = [&]{ return b; };
static_assert(is_constexpr(cap1));
static_assert(is_constexpr(cap2));
static_assert(is_constexpr(cap3));
static_assert(is_constexpr(cap2()));
static_assert(is_constexpr(cap3()));
// non-constexpr capture lambda
auto cap4 = [&]{};
auto cap5 = [&]{ return c; };
auto cap6 = [&]{ return d; };
auto cap7 = [&]{ return e; };
static_assert(is_constexpr(cap4));
static_assert(!is_constexpr(cap5));
static_assert(!is_constexpr(cap6));
static_assert(!is_constexpr(cap7));
static_assert(!is_constexpr(cap5()));
static_assert(!is_constexpr(cap6()));
static_assert(!is_constexpr(cap7()));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment