Skip to content

Instantly share code, notes, and snippets.

@kimyongin
Last active February 1, 2016 06:21
Show Gist options
  • Save kimyongin/c0416d5980d70fe69224 to your computer and use it in GitHub Desktop.
Save kimyongin/c0416d5980d70fe69224 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
namespace my
{
namespace assign_detail
{
template< class C >
class call_push_back
{
C& c_;
public:
call_push_back(C& c) : c_(c)
{ }
template< class T >
void operator()(T r)
{
c_.push_back(r);
}
};
struct forward_n_arguments {};
}
namespace assign
{
template< class Function, class Argument = assign_detail::forward_n_arguments >
class list_inserter
{
public:
list_inserter(Function fun) : insert_(fun)
{}
template< class T >
list_inserter& operator,(const T& r)
{
insert_(r);
return *this;
}
template< class T >
list_inserter& operator()(const T& t)
{
insert_(t);
return *this;
}
private:
Function insert_;
};
template< class Function, class Argument >
inline list_inserter<Function, Argument>
make_list_inserter(Function fun, Argument*)
{
return list_inserter<Function, Argument>(fun);
}
template< class C >
inline list_inserter< assign_detail::call_push_back<C>, typename C::value_type >
push_back(C& c)
{
static typename C::value_type* p = 0;
return make_list_inserter(assign_detail::call_push_back<C>(c), p);
}
template< class V, class A, class V2 >
inline list_inserter< assign_detail::call_push_back< std::vector<V, A> >, V >
operator+=(std::vector<V, A>& c, V2 v)
{
return push_back(c)(v);
}
}
}
int main()
{
using namespace my::assign;
std::vector<int> input;
input += 1, 1, 2, 2, 2, 3, 4, 5, 6;
for(auto item : input)
{
std::cout << item << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment