Skip to content

Instantly share code, notes, and snippets.

@juxeii
juxeii / Optional.hpp
Last active January 25, 2021 08:48
Blog post for Optional
auto nameByShirtNo = std::map<int, std::string>{{8, "Andres Iniesta"}};
const auto shirtNumber = 8;
const auto nameExists = nameByShirtNo.contains(shirtNumber);
if (nameExists)
{
const auto playerName = nameByShirtNo.at(shirtNumber);
std::cout << playerName << " wears shirt number " << shirtNumber;
}
else
@juxeii
juxeii / MemoizerPart1.hpp
Last active January 14, 2021 13:21
Blog post for Memoization part 1
auto foo(int n, std::string s)
{
// ... calculate something expensive(like the answer to everything)
return 42;
}
auto fooM(int n, std::string s)
{
// what goes in here?
}
@juxeii
juxeii / CallableTraits.hpp
Last active December 31, 2020 11:56
Blog post for CallableTraits
template <typename T>
using AsFunction = decltype(std::function{std::declval<T>()});
#if defined(__GNUC__) || defined(__GNUG__)
// CTAD bug in gcc, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98077
#define AS_FUNCTION(T) decltype(std::function{std::declval<T>()})
#else
#define AS_FUNCTION(T) AsFunction<T>
#endif