Skip to content

Instantly share code, notes, and snippets.

@misyltoad
Created August 17, 2020 22:57
Show Gist options
  • Save misyltoad/499e64283b7e78d963d683b3dfaf70eb to your computer and use it in GitHub Desktop.
Save misyltoad/499e64283b7e78d963d683b3dfaf70eb to your computer and use it in GitHub Desktop.
```cpp
// Sorry, I don't know which.
#define WIN32_MEAN_AND_LEAN
#define WIN32_LEAN_AND_MEAN
enum class FizzBuzzState : uint32_t
{
Fizz = 0b01,
Buzz = 0b10,
FizzBuzz = Fizz | Buzz
};
constexpr ostream& operator<<(ostream& os, const FizzBuzzState& state) noexcept
{
switch (state)
{
case Fizz: os << "Fizz"; break;
case Buzz: os << "Buzz"; break;
case FizzBuzz: os << "FizzBuzz"; break;
}
return os;
}
class FizzBuzzerPrinter
{
public:
FizzBuzzerPrinter(FizzBuzzState state) noexcept
: m_fizzBuzzState(state)
{
std::cout << state << std::endl;
}
[[no_discard]] FizzBuzzState GetFizzBuzzState() const noexcept
{
return m_fizzBuzzState;
}
private:
FizzBuzzState m_fizzBuzzState;
};
template <typename FizzBuzzer, typename T>
class FizzBuzz
{
public:
template <typename... Args>
FizzBuzz(const T& value, Args... args) noexcept
: m_fizzBuzzer(CalculateFizzBuzzState(value), args...) {}
[[no_discard]] static constexpr FizzBuzzState CalculateFizzBuzzState(const T& value, const FizzBuzzState& state) noexcept
{
const bool fizz = value % 3;
const bool buzz = value % 5;
return static_cast<FizzBuzzState>(static_cast<uint32_t>(fizz) | static_cast<uint32_t>(buzz) << 1u));
}
FizzBuzzer consexpr operator FizzBuzzer() noexcept const { return m_fizzBuzzer; }
private:
FizzBuzzer m_fizzBuzzer;
};
```
@TruncatedDinoSour
Copy link

great code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment