Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
dgodfrey206 / gist:7648972
Last active December 29, 2015 09:18
Dispatching methods from instances.
#include <iostream>
#include <functional>
#include <string>
#include <map>
class X;
template<class X>
class dispatch_traits;
@dgodfrey206
dgodfrey206 / gist:7754537
Last active December 30, 2015 01:09
Writing output for a Car class
#include <iostream>
#include <string>
using namespace std;
class Car
{
public:
Car(const basic_string<char>& _name, int _year)
: name(_name), year(_year)
@dgodfrey206
dgodfrey206 / gist:7788108
Created December 4, 2013 14:17
Inserter for a Car object.
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
class Car
{
public:
Car(const basic_string<char>& _name, int _year)
: name(_name), year(_year)
@dgodfrey206
dgodfrey206 / gist:7938186
Last active December 31, 2015 04:59
Inserter/Extractor for a Line object
#include <iostream>
#include <utility>
#include <tuple>
#include <sstream>
#include <typeinfo>
#include <vector>
using namespace std;
class Point
{
@dgodfrey206
dgodfrey206 / gist:8307750
Created January 7, 2014 22:03
Subset function
#include <iostream>
#include <string>
template< class ForwardIt1, class ForwardIt2 >
bool is_subset(ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2)
{
auto initial = first2;
for (; (first1 != last1) && (first2 != last2); ++first1)
@dgodfrey206
dgodfrey206 / gist:8366305
Created January 11, 2014 02:40
Synchronization of locales between streams.
#include <iostream>
#include <sstream>
static int tied_stream()
{
static int i = std::ios_base::xalloc(); return i;
}
static int first_check()
{
@dgodfrey206
dgodfrey206 / gist:8410778
Created January 14, 2014 00:29
Manipulator base class
#include <iostream>
#include <functional>
class manipulator
{
public:
template<typename Stream, typename F, typename... Args>
manipulator(Stream& os, F&& f, Args&&... args)
: callback(std::bind(std::forward<F>(f), std::ref(os), std::forward<Args>(args)...))
{ callback();}
@dgodfrey206
dgodfrey206 / gist:8447788
Last active January 3, 2016 10:09
Opting in or out of the flushing mechanism of std::endl
#include <iostream>
static int disable() {
static int index(std::ios_base::xalloc());
return index;
}
class save_buffer
{
public:
@dgodfrey206
dgodfrey206 / gist:8457070
Last active January 3, 2016 11:38
A manipulator that allows you to specify how many new lines you want written to the output stream at one time.
#include <iostream>
class endl_n_manipulator
{
public:
endl_n_manipulator(int count) : count(count) { }
friend std::ostream& operator<<(std::ostream& os, const endl_n_manipulator& manip)
{
std::ostream::sentry s(os);
@dgodfrey206
dgodfrey206 / gist:8853181
Created February 6, 2014 21:48
Reversing the content of an input stream.
#include <iostream>
#include <sstream>
#include <vector>
#include <iterator>
template<class charT>
class reverse_buffer : public std::basic_stringbuf<charT>
{
typedef std::basic_stringbuf<charT> streambuf_type;
typedef typename streambuf_type::int_type int_type;