Skip to content

Instantly share code, notes, and snippets.

@sithhell
Created May 7, 2012 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sithhell/2627795 to your computer and use it in GitHub Desktop.
Save sithhell/2627795 to your computer and use it in GitHub Desktop.
minimal Fusion splat implementation
#include <boost/fusion/include/size.hpp>
#include <boost/fusion/include/at_c.hpp>
#include <boost/move/move.hpp>
namespace tmp
{
template <typename Seq, int Size = boost::fusion::result_of::size<Seq>::value>
struct make_splat_impl;
template <typename Seq>
struct make_splat_impl<Seq, 0>
{
Seq operator()(...) const
{
return Seq();
}
};
template <typename Seq>
struct make_splat_impl<Seq, 1>
{
template <typename T0>
Seq operator()(BOOST_FWD_REF(T0) t0) const
{
return
Seq(
typename boost::fusion::result_of::value_at_c<Seq, 0>::type(
boost::forward<T0>(t0)
)
);
}
template <typename T0, typename T1>
Seq operator()(BOOST_FWD_REF(T0) t0, BOOST_FWD_REF(T1) t1) const
{
return
Seq(
typename boost::fusion::result_of::value_at_c<Seq, 0>::type(
boost::forward<T0>(t0)
, boost::forward<T1>(t1)
)
);
}
// and so on ...
};
template <typename Seq>
struct make_splat_impl<Seq, 2>
{
template <typename T0>
Seq operator()(BOOST_FWD_REF(T0) t0) const
{
return
Seq(
typename boost::fusion::result_of::value_at_c<Seq, 0>::type(
boost::forward<T0>(t0)
)
, typename boost::fusion::result_of::value_at_c<Seq, 1>::type(
boost::forward<T0>(t0)
)
);
}
template <typename T0, typename T1>
Seq operator()(BOOST_FWD_REF(T0) t0, BOOST_FWD_REF(T1) t1) const
{
return
Seq(
typename boost::fusion::result_of::value_at_c<Seq, 0>::type(
boost::forward<T0>(t0)
, boost::forward<T1>(t1)
)
, typename boost::fusion::result_of::value_at_c<Seq, 1>::type(
boost::forward<T0>(t0)
, boost::forward<T1>(t1)
)
);
}
// and so on ...
};
// and so on ...
template <typename Seq, typename T0>
Seq make_splat(BOOST_FWD_REF(T0) t0)
{
return make_splat_impl<Seq>()(boost::forward<T0>(t0));
}
template <typename Seq, typename T0, typename T1>
Seq make_splat(T0 t0, T1 t1)
{
return make_splat_impl<Seq>()(boost::forward<T0>(t0), boost::forward<T1>(t1));
}
// and so on ...
}
#include <boost/fusion/include/vector.hpp>
#include <vector>
int main()
{
typedef boost::fusion::vector<int> seq1;
typedef boost::fusion::vector<std::vector<int>, std::vector<double> > seq2;
seq1 s1 = tmp::make_splat<seq1>(8);
seq2 s2 = tmp::make_splat<seq2>(8);
seq2 s3 = tmp::make_splat<seq2>(8, 8);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment