How does one use concepts in if constexpr?
Given the example below, what would one give to if constexpr to return 1 in case T meets the requirements of integral and else 0?
template<typename T>
concept integral = std::is_integral_v<T>;
struct X{};
template<typename T>
constexpr auto a () {
if constexpr (/* T is integral */) {
return 1;
}
else {
return 0;
}
}
int main () {
return a<X>();
}
Concepts are named boolean predicates on template parameters, evaluated at compile time.
In a constexpr if statement, the value of the condition must be a contextually converted constant expression of type bool.
So in this case, usage is simple:
if constexpr ( integral<T> )
It is sufficient to do:
if constexpr ( integral<T> )
since integral<T> is already testable as bool
Related
I am trying to understand the utility of if constexpr and want to know if there is any utility in using it in this way.
template<bool B>
int fun()
{
if constexpr (B)
return 1;
return 0;
}
Is this function changed at all by using if constexpr instead of a regular if? I assume the performance would be the same. My understanding of templates is that the outcome of the if statement is already known at compile time so there is no difference.
Utility of constexpr?
A trivial example... if you write the following function
template <typename T>
auto foo (T const & val)
{
if ( true == std::is_same_v<T, std::string>> )
return val.size()
else
return val;
}
and call it with an integer
foo(42);
you get a compilation error, because the instruction
val.size();
has to be instantiated also when val is an int but, unfortunately, int isn't a class with a size() method
But if you add constexpr after the if
// VVVVVVVVV
if constexpr ( true == std::is_same_v<T, std::string>> )
return val.size()
now the return val.size(); instruction is instantiated only when T is std::string, so you can call foo() also with arguments without a size() method.
---- EDIT ----
As #prapin observed in a comment (thanks!), if constexpr can be necessary for an auto function.
I propose another trivial (and silly) example
Without if constexpr, the following bar() function
template <typename T>
auto bar (T const &)
{
if ( true == std::is_same_v<T, std::string>> )
return 42;
else
return 42L;
}
doesn't compile, because the first return return a int value, the second return a long; so, given that without if constexpr the compiler must instantiate both return's, so the compiler can't conciliate the returns types and can't determine the return type of the function.
With if constexpr,
if constexpr ( true == std::is_same_v<T, std::string>> )
return 42;
else
return 42L;
the compiler instantiate the first return or the second one; never both. So the compiler ever determine the type returned from the function (int when called with a std::string, long otherwise).
In the following example the requires-expression of second f-function overload has the type std::integral_constant<bool,true>, which is implicitly convertible to bool:
#include <type_traits>
struct S {
static constexpr bool valid = true;
};
template<typename T>
int f() { return 1; }
template<typename T>
int f() requires( std::bool_constant< T::valid >() ) { return 2; }
int main() {
return f<S>();
}
One can observe that GCC rejects the program due to the type is not precisely bool, but Clang accepts, but selects the other overload int f() { return 1; }. Demo: https://gcc.godbolt.org/z/nf65zrxoK
Which compiler is correct here?
I believe GCC is correct—the type must be bool exactly per [temp.constr.atomic]/3 (note that E here is std::bool_constant< T::valid >()):
To determine if an atomic constraint is satisfied, the parameter mapping and template arguments are first substituted into its expression. If substitution results in an invalid type or expression, the constraint is not satisfied. Otherwise, the lvalue-to-rvalue conversion is performed if necessary, and E shall be a constant expression of type bool. The constraint is satisfied if and only if evaluation of E results in true. If, at different points in the program, the satisfaction result is different for identical atomic constraints and template arguments, the program is ill-formed, no diagnostic required. [ Example:
template<typename T> concept C =
sizeof(T) == 4 && !true; // requires atomic constraints sizeof(T) == 4 and !true
template<typename T> struct S {
constexpr operator bool() const { return true; }
};
template<typename T> requires (S<T>{})
void f(T); // #1
void f(int); // #2
void g() {
f(0); // error: expression S<int>{} does not have type bool
} // while checking satisfaction of deduced arguments of #1;
// call is ill-formed even though #2 is a better match
— end example ]
I was messing around with c++20 consteval in GCC 10 and wrote this code
#include <optional>
#include <tuple>
#include <iostream>
template <std::size_t N, typename Predicate, typename Tuple>
consteval std::optional<std::size_t> find_if_impl(Predicate&& pred,
Tuple&& t) noexcept {
constexpr std::size_t I = std::tuple_size_v<std::decay_t<decltype(t)>> - N;
if constexpr (N == 0u) {
return std::nullopt;
} else {
return pred(std::get<I>(t))
? std::make_optional(I)
: find_if_impl<N - 1u>(std::forward<decltype(pred)>(pred),
std::forward<decltype(t)>(t));
}
}
template <typename Predicate, typename Tuple>
consteval std::optional<std::size_t> find_if(Predicate&& pred,
Tuple&& t) noexcept {
return find_if_impl<std::tuple_size_v<std::decay_t<decltype(t)>>>(
std::forward<decltype(pred)>(pred), std::forward<decltype(t)>(t));
}
constexpr auto is_integral = [](auto&& x) noexcept {
return std::is_integral_v<std::decay_t<decltype(x)>>;
};
int main() {
auto t0 = std::make_tuple(9, 1.f, 2.f);
constexpr auto i = find_if(is_integral, t0);
if constexpr(i.has_value()) {
std::cout << std::get<i.value()>(t0) << std::endl;
}
}
Which is supposed to work like the STL find algorithm but on tuples and instead of returning an iterator, it returns an optional index based on a compile time predicate. Now this code compiles just fine and it prints out
9
But if the tuple does not contain an element that's an integral type, the program doesn't compile, because the i.value() is still called on an empty optional. Now why is that?
This is just how constexpr if works. If we check [stmt.if]/2
If the if statement is of the form if constexpr, the value of the condition shall be a contextually converted constant expression of type bool; this form is called a constexpr if statement. If the value of the converted condition is false, the first substatement is a discarded statement, otherwise the second substatement, if present, is a discarded statement. During the instantiation of an enclosing templated entity ([temp.pre]), if the condition is not value-dependent after its instantiation, the discarded substatement (if any) is not instantiated.[...]
emphasis mine
So we can see that we only do not evaluate the discarded expression if we are in a template and if the condition is value-dependent. main is not a function template so the body of the if statement is still checked by the compiler for correctness.
Cppreference also says this in their section about constexpr if with:
If a constexpr if statement appears inside a templated entity, and if condition is not value-dependent after instantiation, the discarded statement is not instantiated when the enclosing template is instantiated .
template<typename T, typename ... Rest>
void g(T&& p, Rest&& ...rs) {
// ... handle p
if constexpr (sizeof...(rs) > 0)
g(rs...); // never instantiated with an empty argument list.
}
Outside a template, a discarded statement is fully checked. if constexpr is not a substitute for the #if preprocessing directive:
void f() {
if constexpr(false) {
int i = 0;
int *p = i; // Error even though in discarded statement
}
}
I am trying to pass a more "generic" const input parameter to a constexpr implementation for fibonacci. When I replace the template parameter with an int, things are hunky-dory again.
#include<iostream>
template <typename T>
constexpr auto fib_ce(T n) {
return (n>1) ? fib_ce(n-1)+fib_ce(n-2) : 1;
}
int main() {
std::cout<<fib_ce(4)<<"\n";
}
This is the error I get:
g++ -std=c++14 -o constexpr_fib constexpr_fib.cpp
constexpr_fib.cpp:4:19: fatal error: recursive template instantiation exceeded maximum depth of 256
return (n>1) ? fib_ce(n-1)+fib_ce(n-2) : 1;
^
How do I provide a template argument to a constexpr, that can take inputs like long, int, unsigned long, etc etc for this constexpr
The rule in [dcl.spec.auto] is:
If the type of an entity with an undeduced placeholder type is needed to determine the type of an expression,
the program is ill-formed.
This is to just cut short the arbitrary complexity that could be infinite recursive deduction. Fear not though, there are ways around this problem:
Just use T instead of auto:
template <class T>
constexpr T fib_ce(T n) {
return (n>1) ? fib_ce(n-1)+fib_ce(n-2) : 1;
}
We also have the rule:
Once a non-discarded return statement has been seen in a function, however, the return type deduced from that statement can be used in the rest of the function, including in other return
statements.
So we can use an if statement instead of the conditional operator. We just have to invert the logic so that the return statement with known type goes first:
template <typename T>
constexpr auto fib_ce(T n) {
if (n <= 1) {
return static_cast<T>(1); // ok, deduced as T
}
else {
return fib_ce(n-1)+fib_ce(n-2); // we already deduced T, so sticking with it
}
}
Ok, I think I found the answer, need to refrain from auto and letting the compiler work the return type out here. The following works fine:
#include<iostream>
template <typename T>
constexpr T fib_ce(T n) {
return (n>1) ? fib_ce(n-1)+fib_ce(n-2) : 1;
}
int main() {
std::cout<<fib_ce(4)<<"\n";
}
Maybe I missed something, but I can't find any hints: is there a constexpr ternary operator in C++17 equivalent to constexpr-if?
template<typename Mode>
class BusAddress {
public:
explicit constexpr BusAddress(Address device) :
mAddress(Mode::write ? (device.mDevice << 1) : (device.mDevice << 1) | 0x01) {}
private:
uint8_t mAddress = 0;
};
No, there is no constexepr conditional operator. But you could wrap the whole thing in a lambda and immediately evaluate it (an IIFE):
template<typename Mode>
class BusAddress {
public:
explicit constexpr BusAddress(Address device)
: mAddress([&]{
if constexpr (Mode::write) {
return device.mDevice << 1;
}
else {
return (device.mDevice << 1) | 0x01;
}
}())
{ }
private:
uint8_t mAddress = 0;
};
It may not be the sexiest code ever, but it gets the job done. Note that lambdas are constexpr by default where possible as of N4487 and P0170.
You seem to be acting under the belief that if constexpr is a performance optimization. It isn't. If you put a constant expression in a ?: clause, any compiler worth using will figure out what it resolves to and remove the condition. So the code as you have written it will almost certainly compile down to a single option, for a particular Mode.
The principle purpose of if constexpr is to eliminate the other branch entirely. That is, the compiler doesn't even check to see if it is syntactically valid. This would be for something where you if constexpr(is_default_constructible_v<T>), and if it is true, you do T(). With a regular if statement, if T isn't default constructible, T() will still have to be syntactically valid code even if the surrounding if clause is a constant expression. if constexpr removes that requirement; the compiler will discard statements that are not in the other condition.
This becomes even more complicated for ?:, because the expression's type is based on the types of the two values. As such, both expressions need to be legal expressions, even if one of them is never evaluated. A constexpr form of ?: would presumably discard the alternative that is not taken at compile time. And therefore the expression's type should really only be based on one of them.
That a very different kind of thing.
Accepted answer can also be translated into a template function for convenience:
#include <type_traits>
#include <utility>
template <bool cond_v, typename Then, typename OrElse>
decltype(auto) constexpr_if(Then&& then, OrElse&& or_else) {
if constexpr (cond_v) {
return std::forward<Then>(then);
} else {
return std::forward<OrElse>(or_else);
}
}
// examples
struct ModeFalse { static constexpr bool write = false; };
struct ModeTrue { static constexpr bool write = true; };
struct A {};
struct B {};
template <typename Mode>
auto&& test = constexpr_if<Mode::write>(A{}, B{});
static_assert(std::is_same_v<A&&, decltype(test<ModeTrue>)>);
static_assert(std::is_same_v<B&&, decltype(test<ModeFalse>)>);
const A a;
B b;
template <typename Mode>
auto&& test2 = constexpr_if<Mode::write>(a, b);
static_assert(std::is_same_v<const A&, decltype(test2<ModeTrue>)>);
static_assert(std::is_same_v<B&, decltype(test2<ModeFalse>)>);