This is the collection of snippets I found on internet or made by me.
- Requirement: C++11 or later.
- Source: pmttavaras's answer on stackoverflow.
- Modified to use move-semantic for true zero-overhead by me.
Deferred functions will be called at the end of the scope where they were declared, in last-in-first-out order.
Snippet:
#include <memory>
struct defer_dummy {};
template<class F>
struct deferer
{
F _f;
deferer(F&& f) noexcept : _f(f) {}
~deferer() { _f(); }
};
template<class F>
inline deferer<F> operator*(defer_dummy, F&& f) noexcept { return deferer<F>(std::move(f)); }
#define DEFER_(LINE) zz_defer##LINE
#define DEFER(LINE) DEFER_(LINE)
#define defer auto DEFER(__LINE__) = defer_dummy{} *[&]()
Sample:
int main()
{
defer{ printf("4\n"); };
defer{ printf("3\n"); };
{
defer{ printf("1\n"); };
}
defer{ printf("2\n"); };
}
- Source: ifreilicht's answer on stackoverflow.
- Modified to be more efficient by me.
Snippet:
#include <cstdio>
#include <memory>
#include <stdexcept>
#include <string>
namespace nstd
{
template<typename... Args>
std::string format(const std::string& format, const Args&... args)
{
int size_s = std::snprintf(nullptr, 0, format.c_str(), args...);
if (size_s < 0) throw std::runtime_error("Error during formatting");
std::string ret(size_s, '\x00');
std::snprintf(&ret[0], size_s + 1, format.c_str(), args...);
return ret;
}
}
Sample:
int main()
{
std::cout << nstd::format("hello %s!\n", "lebaoworks");
}
- Source: Me.
- Requirement: format
Snippet:
#include <stdexcept>
#include <string>
namespace nstd
{
struct runtime_error : public std::runtime_error
{
template<typename... Args>
runtime_error(const std::string& format, const Args&... args) :
std::runtime_error(nstd::format(format, args...)) {}
};
}
Sample:
int main()
{
try
{
throw nstd::runtime_error("test except by %s", "lebaoworks");
}
catch (std::exception& e)
{
printf("Got exception: %s", e.what());
}
}