Skip to content

Instantly share code, notes, and snippets.

@robertmaynard
Created January 2, 2013 15:09
Show Gist options
  • Save robertmaynard/4435237 to your computer and use it in GitHub Desktop.
Save robertmaynard/4435237 to your computer and use it in GitHub Desktop.
simple example of a fusion style rtim of a tuple.
namespace detail
{
//create a Factory item with only the first N items in it
template< template<class ...> class Factory,
int TruncateSize,
int ItemsToDrop,
class T,
class ...OtherArgs>
struct rtrim
{
typedef typename rtrim<Factory,TruncateSize,ItemsToDrop-1,OtherArgs...,T>::type type;
type operator()(T t, OtherArgs... args) const
{
return rtrim<Factory,TruncateSize,ItemsToDrop-1,OtherArgs...,T>()(args...,t);
}
};
//create a Factory item with only the first N items in it
template<template<class ...> class Factory, int TruncateSize, class T, class ...OtherArgs>
struct rtrim<Factory, TruncateSize, 0, T, OtherArgs...>
{
enum{M = sizeof...(OtherArgs) - TruncateSize};
typedef typename ltrim<Factory,M,OtherArgs...,T>::type type;
type operator()(T t, OtherArgs... args) const
{
return ltrim<Factory,M,OtherArgs...,T>()(args...,t);
}
};
}
//create a Factory item with only the first N items in it, aka rtrim
template< template<class ...> class Factory,
int TruncateSize,
class T,
class ...OtherArgs>
struct rtrim
{
typedef typename detail::rtrim<Factory,TruncateSize,TruncateSize-1,OtherArgs...,T>::type type;
type operator()(T t, OtherArgs... args) const
{
return detail::rtrim<Factory,TruncateSize,TruncateSize-1,OtherArgs...,T>()(args...,t);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment