xtd 1.0.0
Loading...
Searching...
No Matches
if_then_else_expression.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "as_expression.hpp"
9#include <utility>
10
12namespace xtd {
14 namespace expressions {
29 template <typename condition_t, typename then_t, typename else_t>
30 struct if_then_else_expression : conditional_expression {
32
37
39
42 constexpr if_then_else_expression() = default;
43
48 constexpr if_then_else_expression(condition_t condition, then_t then_, else_t else_) : condition(std::move(condition)), then_(std::move(then_)), else_(std::move(else_)) {}
50
52
57 template <typename... args_t>
58 constexpr auto operator()(args_t&&... args) const {return condition(args...) ? then_(args...) : else_(args...);}
60
62 friend inline auto operator <<(std::ostream& os, const if_then_else_expression& e) -> std::ostream& {return os << expression_stream {e.condition, e.precedence} << " ? " << expression_stream {e.then_, e.precedence} << " : " << expression_stream {e.else_, e.precedence};}
64
65 private:
66 [[no_unique_address]] condition_t condition;
67 [[no_unique_address]] then_t then_;
68 [[no_unique_address]] else_t else_;
69 };
70
72
90 template <typename condition_t, typename then_t, typename else_t>
92 constexpr auto if_then_else(condition_t condition, then_t then_, else_t else_) {
93 auto condition_expression = as_expression(condition);
94 auto then_expression = as_expression(then_);
95 auto else_expression = as_expression(else_);
96 return if_then_else_expression<std::decay_t<decltype(condition_expression)>, std::decay_t<decltype(then_expression)>, std::decay_t<decltype(else_expression)>> {std::move(condition_expression), std::move(then_expression), std::move(else_expression)};
97 }
98
99 }
100}
Contains xtd::expressions::as_expression methods.
The xtd::expressions::expression_operand object is expression operand concept.
Definition expression_operand.hpp:27
Contains xtd::expressions::conditional_expression struct.
Contains xtd::expressions::expression_operand concept.
Contains xtd::expressions::expression_stream struct.
constexpr auto operator<<(left_t left, right_t right)
Subtract the specified left and right operands.
Definition left_shift.hpp:127
operator_precedence
Specifies the operator precedence.
Definition operator_precedence.hpp:22
constexpr auto if_then_else(condition_t condition, then_t then_, else_t else_)
If condition is true return then_ else retur nelse_.
Definition if_then_else_expression.hpp:92
@ ternary
Represnets the ternary operator precedence (a ? a : c).
Definition operator_precedence.hpp:120
constexpr decltype(auto) as_expression(type_t &&value)
The xtd::expressions::as_expression method convert a type as xtd::expressions::expression.
Definition as_expression.hpp:33
@ e
The E key.
Definition console_key.hpp:96
The xtd::expressions namespace provides a lightweight, composable expression template framework for b...
Definition add.hpp:14
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Generates a set of positional args that can be decomposed using structured bindings to build readable...
Definition args.hpp:42
The xtd::expressions::expression_stream is the streamable class for expression objects.
Definition expression_stream.hpp:23
The xtd::expressions::if_then_else_expression is the addition expression.
Definition if_then_else_expression.hpp:30
static constexpr operator_precedence precedence
The operator precedence. That contains one of xtd::expressions::operator_precedence values.
Definition if_then_else_expression.hpp:35
constexpr auto operator()(args_t &&... args) const
Equal to the specified arguments.
Definition if_then_else_expression.hpp:58
constexpr if_then_else_expression()=default
Initialize a new xtd::expressions::if_then_else_expression object.
constexpr if_then_else_expression(condition_t condition, then_t then_, else_t else_)
Initialize a new xtd::expressions::logical_or_expression object with specified left and right operand...
Definition if_then_else_expression.hpp:48