ftmpl::vectorでpush_backしてみた。

/sandbox/ftmpl


本日、BoostCon '09でこれの発表があったそうです。


テンプレートメタプログラミングが<>から()になりました。
decltypeばんざい。

#include <iostream>
#include <cxxabi.h>
#include "vector.hpp"
#include "type.hpp"
#include "unwrap.hpp"
#include "id.hpp"
#include "is_same.hpp"
#include "apply.hpp"

char* demangle(const char *demangle) {
    int status;
    return abi::__cxa_demangle(demangle, 0, 0, &status);
}

int main()
{
    using namespace boost::ftmpl;

    typedef decltype(vector(int_<3>(), int_<1>(), int_<4>(), int_<5>())) expected;

    typedef decltype(vector(int_<3>(), int_<1>(), int_<4>())) v;
    typedef decltype(push_back(id<v>(), int_<5>()))           result;

    static_assert(unwrap_value<BOOST_FTMPL_UNWRAP(
        apply(is_same, id<result>(), id<expected>())
    )>::value, "failed");

    std::cout << demangle(typeid(result).name())   << std::endl;
    std::cout << demangle(typeid(expected).name()) << std::endl;
}
boost::ftmpl::type_t<boost::ftmpl::vector_t<boost::ftmpl::type_t<boost::ftmpl::value_t<int, 3> >, boost::ftmpl
::type_t<boost::ftmpl::value_t<int, 1> >, boost::ftmpl::type_t<boost::ftmpl::value_t<int, 4> >, boost::ftmpl::
type_t<boost::ftmpl::value_t<int, 5> > > >
boost::ftmpl::type_t<boost::ftmpl::vector_t<boost::ftmpl::type_t<boost::ftmpl::value_t<int, 3> >, boost::ftmpl
::type_t<boost::ftmpl::value_t<int, 1> >, boost::ftmpl::type_t<boost::ftmpl::value_t<int, 4> >, boost::ftmpl::
type_t<boost::ftmpl::value_t<int, 5> > > >


部品が足りなかったのでidを作った。
型を値として渡す用。

// id.hpp
#ifndef BOOST_FTMPL_ID_INCLUDE
#define BOOST_FTMPL_ID_INCLUDE

namespace boost { namespace ftmpl {

template <class T>
T& id();

}}

#endif // BOOST_FTMPL_ID_INCLUDE