lanzkron
-
C++,
pasted
on Jul 27:
|
#include <boost/mpl/list.hpp>
template <class List>
struct foo{
template <class T>
static void* bar(T* obj, size_t size)
{
typedef typename boost::mpl::front<List>::type type;
if (sizeof(type) == size)
return reinterpret_cast<type*>(obj);
// Otherwise check the rest of the list
return foo<typename List::next>::bar(obj, size);
}
};
template <>
struct foo<boost::mpl::list0<boost::mpl::na> >
{
template <class T>
static void* bar(T*)
{
return NULL;
}
};
#include <iostream>
int main()
{
int n = 3;
void *p = foo<boost::mpl::list<char, bool, double, long> >::bar(&n, 4);
std::cout << p << std::endl;
}
|
Output:
|
/usr/local/include/boost/mpl/list/aux_/front.hpp: In instantiation of 'boost::mpl::front_impl<boost::mpl::aux::list_tag>::apply<boost::mpl::l_end>':
/usr/local/include/boost/mpl/front.hpp:31: instantiated from 'boost::mpl::front<boost::mpl::l_end>'
t.cpp:9: instantiated from 'static void* foo<List>::bar(T*, size_t) [with T = int, List = boost::mpl::l_end]'
t.cpp:14: instantiated from 'static void* foo<List>::bar(T*, size_t) [with T = int, List = boost::mpl::list1<long int>]'
t.cpp:14: instantiated from 'static void* foo<List>::bar(T*, size_t) [with T = int, List = boost::mpl::list2<double, long int>]'
t.cpp:14: instantiated from 'static void* foo<List>::bar(T*, size_t) [with T = int, List = boost::mpl::list3<bool, double, long int>]'
t.cpp:14: instantiated from 'static void* foo<List>::bar(T*, size_t) [with T = int, List = boost::mpl::list<char, bool, double, long int, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na>]'
t.cpp:33: instantiated from here
Line 26: error: no type named 'item' in 'struct boost::mpl::l_end'
compilation terminated due to -Wfatal-errors.
|
|