Quantcast
Channel: User HolyBlackCat - Stack Overflow
Viewing all articles
Browse latest Browse all 1285

How to check that a function result is constexpr even when the argument is not?

$
0
0

constexpr functions can yield constexpr results even when their arguments are not constexpr, if they are unused. Such is, for example, std::integral_constant::operator T:

#include <utility>std::integral_constant<int, 42> a;constexpr int b = a; // Compiles!

How can I make a concept to test this property of a function?

For example, I want the following to work: run on gcc.godbolt.org

#include <utility>struct A {explicit constexpr operator bool() const {return true;}};struct B {int x = 1; explicit constexpr operator bool() const {return x;}};template <typename T>concept C = requires(T t){std::bool_constant<t>{};};static_assert(C<A>);static_assert(!C<B>);

But this gives me:

<source>:7:46: error: constraint variable 't' cannot be used in an evaluated context    7 | concept C = requires(T t){std::bool_constant<t>};      |                                              ^

Viewing all articles
Browse latest Browse all 1285

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>