TMP magic to support gcc 4.7

This commit is contained in:
Daniel Sipka 2015-04-26 18:22:42 +02:00
parent 39748d3a60
commit f8e23fd9ef

View File

@ -8,6 +8,9 @@
#include <boost/variant.hpp> #include <boost/variant.hpp>
namespace mstch { namespace mstch {
using renderer = std::function<std::string(const std::string&)>;
namespace internal { namespace internal {
template<class N> template<class N>
@ -34,19 +37,38 @@ class object_t {
mutable std::map<std::string, N> cache; mutable std::map<std::string, N> cache;
}; };
} template <typename T>
class is_fun {
private:
using not_fun = char;
using fun_without_args = char[2];
using fun_with_args = char[3];
template <typename U, U> struct really_has;
template <typename C> static fun_without_args& test(
really_has<std::string(C::*)() const, &C::operator()>*);
template <typename C> static fun_with_args& test(
really_has<std::string(C::*)(const std::string&,renderer) const,
&C::operator()>*);
template <typename> static not_fun& test(...);
using renderer = std::function<std::string(const std::string&)>; public:
static bool const no_args = sizeof(test<T>(0)) == sizeof(fun_without_args);
static bool const has_args = sizeof(test<T>(0)) == sizeof(fun_with_args);
};
}
class lambda { class lambda {
public: public:
lambda(std::function<std::string()> fun): template<class F>
fun([fun](const std::string&, renderer){return fun();}) lambda(F f, typename std::enable_if<internal::is_fun<F>::no_args>::type* =0):
fun([f](const std::string&,renderer){return f();})
{ {
} }
lambda(std::function<std::string(const std::string&, renderer)> fun): template<class F>
fun(fun) lambda(F f, typename std::enable_if<internal::is_fun<F>::has_args>::type* =0):
fun(f)
{ {
} }