Skip to content

Instantly share code, notes, and snippets.

@gintenlabo
gintenlabo / bracket.cc
Last active December 21, 2015 10:09
bracket on C++11
#include <utility>
namespace etude {
namespace bracket_impl_ {
namespace here = bracket_impl_;
template<class T>
T& as_lvalue(T&& x) {
return x;
@gintenlabo
gintenlabo / get_two_resources.cc
Created August 15, 2013 11:45
get two resources with exceptional safety
template<class T, class D>
auto make_unique_ptr(T* p, D d) noexcept
-> std::unique_ptr<T, D> {
return std::unique_ptr<T, D>(p, std::move(d));
}
auto get_two_resources()
-> std::pair<std::shared_ptr<Res1>, std::shared_ptr<Res2>> {
Res1* res1_ = {};
Res2* res2_ = {};
@gintenlabo
gintenlabo / get_line.cc
Created August 8, 2013 10:12
get line from FILE*
#include <string>
#include <cstring>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#include <boost/optional.hpp>
boost::optional<std::string> get_line(std::FILE* fp) {
std::string s;
#include <boost/utility/string_ref.hpp>
#include <utility>
#include <iostream>
std::size_t all_length(std::initializer_list<boost::string_ref> ss) {
std::size_t n = 0;
for (auto const& s : ss) {
n += s.size();
}
return n;
@gintenlabo
gintenlabo / get_shared_member.cc
Created July 19, 2013 07:46
get shared_ptr which referes (and has ownership of) specified data member
#include <memory>
template<class T, class C>
std::shared_ptr<T> get_shared_member(
std::shared_ptr<C> const& sp, T C::*mp) {
if (!sp) {
return {};
}
auto& x = *sp;
return std::shared_ptr<T>(sp, std::addressof(x.*mp));
@gintenlabo
gintenlabo / copy.cc
Created July 16, 2013 09:29
Etude explicit-copy framework (draft)
#include <type_traits>
#include <utility>
namespace etude {
namespace copy_impl_ {
struct base_ {};
struct derived_ : base_ {};
@gintenlabo
gintenlabo / assert.hpp
Last active December 19, 2015 16:19
Assertion macros
#ifndef INCLUDED_ASSERT_HPP_
#define INCLUDED_ASSERT_HPP_
#ifndef ETUDE_DISABLE_ASSERTIONS
#include <glog/logging.h>
// declares expr to be true
@gintenlabo
gintenlabo / Tape.hs
Last active December 19, 2015 12:29
Haskell でチューリングマシン的な何か inspired by http://d.hatena.ne.jp/its_out_of_tune/20111221/1324491653
module Tape(
Tape(), initTape, moveRight, moveLeft,
readHead, writeHead, modifyHead) where
import Data.List (intercalate)
data InfList a = InfList a (InfList a) | Repeat a
splitHead :: InfList a -> (a, InfList a)
splitHead (InfList a as) = (a, as)

assert 使用のガイドライン

大原則

  • assert で示されている条件は,正しくプログラムが書けている場合,必ず成立する
  • 状況次第で失敗する可能性のある条件を assert にかけてはならない
    • 例として,ユーザ入力や設定ファイルの妥当性検証を assert で行なってはならない
  • 例外にするべきか assert にするべきか迷った場合には,とりあえず例外を使うことを検討する
    • どの例外を投げるのが相応しいかを考える過程で,思考が整理されることが期待できる
  • 思考整理ができなかった場合には,その旨をソースコードや pull req. のコメントに残す
#include <stdexcept>
#include <utility>
#include <cassert>
#if 0
#define NORETURN [[noreturn]]
#else
#define NORETURN
#endif