boost-mpl

How do I convert a C string to a int at compile time?

大兔子大兔子 提交于 2019-12-03 14:52:06
I want to be able to pass an integer or a double (or a string) as a template argument and in some instances convert the result to an integer and use it as a template argument for a type in the class. Here's what I've tried: template <typename MPLString> class A { // the following works fine int fun() { // this function should return the int in the boost mpl type passed to it // (e.g. it might be of the form "123") return std::stoi(boost::mpl::c_str<MPLString>::value); } // the following breaks because std::stoi is not constexpr std::array<int, std::stoi(boost::mpl::c_str<MPLString>::value)>

Creating all template permutations with MPL

a 夏天 提交于 2019-12-03 07:59:33
I have the following templated class structure struct TraitA{}; struct TraitB{}; template<typename trait> struct FunctionalityA{}; template<typename trait> struct FunctionalityB{}; template<typename Func> struct FuncUserA{}; template<typename Func> struct FuncUserB{}; template<typename fuser> struct Host{}; The Host class can now how have the following types. typedef Host<FuncUserA<FunctionalityA<TraitA> > > Host1_t; typedef Host<FuncUserA<FunctionalityA<TraitB> > > Host2_t; typedef Host<FuncUserA<FunctionalityB<TraitA> > > Host3_t; typedef Host<FuncUserA<FunctionalityB<TraitB> > > Host4_t;

How to use std::tuple types with boost::mpl algorithms?

女生的网名这么多〃 提交于 2019-12-03 07:43:00
The boost::mpl algorithms seem not to be able to work on std::tuple types out of the box, e.g., the following does not compile (boost-1.46.0, g++ snapshot 2011-02-19): #include <tuple> #include <boost/mpl/vector.hpp> #include <boost/mpl/contains.hpp> namespace mpl=boost::mpl; typedef mpl::vector<int,float,bool> types; static_assert(mpl::contains<types, float>::value, "vector contains bool"); typedef std::tuple<int,float,bool> types2; // the following does not compile: // error: no class template named ‘apply’ in ‘struct boost::mpl::contains_impl<boost::mpl::non_sequence_tag>’ static_assert(mpl

How to explicitly instantiate a template for all members of MPL vector in C++?

时间秒杀一切 提交于 2019-12-03 04:56:00
问题 Consider the following header file: // Foo.h class Foo { public: template <typename T> void read(T& value); }; I want to explicitly instantiate the Foo::read member function template in a source file for all types included in a boost::mpl::vector : // Foo.cc #include <boost/mpl/vector.hpp> #include <boost/mpl/begin_end.hpp> #include "Foo.h" template <typename T> void Foo::read(T& value) { /* do something */ } typedef boost::mpl::vector<int, long, float> types; // template Foo::read<int >(int&

How to explicitly instantiate a template for all members of MPL vector in C++?

匆匆过客 提交于 2019-12-02 18:14:14
Consider the following header file: // Foo.h class Foo { public: template <typename T> void read(T& value); }; I want to explicitly instantiate the Foo::read member function template in a source file for all types included in a boost::mpl::vector : // Foo.cc #include <boost/mpl/vector.hpp> #include <boost/mpl/begin_end.hpp> #include "Foo.h" template <typename T> void Foo::read(T& value) { /* do something */ } typedef boost::mpl::vector<int, long, float> types; // template Foo::read<int >(int&); // template Foo::read<long >(long&); // template Foo::read<float>(float&); // instantiate

Boost.Variant, Boost.MPL: How to append types?

烈酒焚心 提交于 2019-12-02 18:08:39
问题 I look at this grate code based on boost.Any and cant help but wonder if we could use Boost.Variant instead. I wonder if such API would be possible: void voidFunc() { std::cout << "void called" << std::endl; } int stringFunc(std::string str) { std::cout << str << std::endl; return 0; } int main() { some_map_like_type<std::string, boost::variant> funcs; funcs.insert<void , void >("voidFunc", &voidFunc)); // now our variant vould contain something like boost::function<void, void> funcs.insert

Boost.Variant, Boost.MPL: How to append types?

流过昼夜 提交于 2019-12-02 11:28:38
I look at this grate code based on boost.Any and cant help but wonder if we could use Boost.Variant instead. I wonder if such API would be possible: void voidFunc() { std::cout << "void called" << std::endl; } int stringFunc(std::string str) { std::cout << str << std::endl; return 0; } int main() { some_map_like_type<std::string, boost::variant> funcs; funcs.insert<void , void >("voidFunc", &voidFunc)); // now our variant vould contain something like boost::function<void, void> funcs.insert<int , std::string>("stringFunc", &stringFunc)); // and now we added to our variant a new type: boost:

How to obtain standard mpl sequence after fold

梦想的初衷 提交于 2019-12-01 08:17:55
问题 If I use boost::mpl, lets look at the following code: typedef fold< vector<long,float,long> , set0<> , insert<_1,_2> >::type s; BOOST_MPL_ASSERT_RELATION( size<s>::value, ==, 2 ); How can I turn s into a type t = boost::mpl::set<long,float> again, such that I can use t for selecting a partially specialized template function (on boost::mpl::set``) which extracts the element types and turns it into a std::tuple<long,float> . It is something else 回答1: Here's a full example. I called the

boost::mpl::vector - getting to a type's base-offset

拈花ヽ惹草 提交于 2019-12-01 06:43:12
Is it possible to get at the offset of a mpl::vector after performing a mpl::find<seq,type> on it ? Put differently I want to do the compile time equavalent of: #include <vector> #include <algorithm> #include <iostream> int main() { typedef std::vector<int> v_type; v_type v_int(3); v_int[0] = 1; v_int[1] = 2; v_int[2] = 3; v_type::iterator it= std::find( v_int.begin() ,v_int.end(),3); std::cout << it - v_int.begin() << std::endl; } Failing this, my types in mpl::vector have a type_trait<T>::ordinal const hard-coded, I would like to avoid this if possible. Important Note , I am also creating a

Transforming mpl vector with own function

我怕爱的太早我们不能终老 提交于 2019-12-01 06:12:06
I want to multiply each element in an mpl::vector by an int . First, a metafunction to multiply an int_ with an int . template <int i> struct multiply_scalar { template<typename T> struct apply { typedef int_<(T::value * i)> type; }; }; Here are the calls I want to make. typedef vector<int_<3>, int_<4> > my_vec; typedef typename transform< my_vec, multiply_scalar<2> >::type my_vec_2; typedef vector<int_<6>, int_<8> > my_vec_3; BOOST_MPL_ASSERT(( boost::is_same< my_vec_2, my_vec_3 > )); //Fails //type of my_vec2 is: boost::mpl::v_item<mpl_::int_<8>, boost::mpl::v_item<mpl_::int_<6>, boost::mpl: