It may seem like a dumb question, but why couldn't we somehow put the parameters inside the function's definition, not at the end of it?
For example:
uint16_t Add(uint8_t A)To(uint8_t B)ThenMultiplyWith(uint8_t C) {...}
When called:
Result = Add(12)To(8)ThenMultiplyWith(5);
Is there any workaround using the preprocessors directive?
Any idea or something similar would be appreciated.
Answer
You can get a somewhat similar syntax using a monadic approach
//uint16_t Add(uint8_t A)To(uint8_t B)ThenMultiplyWith(uint8_t C) { ... }
#include <cstdint>
class Expression
{
public:
Expression() = default;
Expression(std::uint16_t v) : value{ v }
{
}
constexpr operator std::uint16_t()
{
return value;
}
constexpr Expression& Add(std::uint8_t v)
{
value += v;
return *this;
}
constexpr Expression& Multiply(std::uint8_t v)
{
value *= v;
return *this;
}
std::uint16_t value{};
};
constexpr Expression Add(std::uint8_t v)
{
return Expression{}.Add(v);
}
int main()
{
constexpr std::uint16_t value = Add(2).Multiply(4);
static_assert(value == 8);
}