c++ - C++11 Create compile time boolean expression with variadic template arguments -


let have following base struct

struct functor {     virtual bool execute()=0; }; 

on base created structs a, b , c. want evaluate boolean expression on variadic list of these objects @ runtime this:

bool evaluate(list<functor> objs) {     for(list<functor>::iterator = objs.begin(); it!=objs.end();++it)     {         if(!it->execute())             return false;     }     return true; } 

my question can having function declared this

template<typename ...f> static bool doevaluation(f... args) {     return... args->execute(); } 

and executing this

doevaluation(new a(), new b(), new c()); 

in end @ compile time?

return new a()->execute() && new b()->execute() && new c()->execute(); 

you can implement after, follows:

#include <functional> // overload single argument template <typename t> constexpr bool check(t&& f) {     return f(); }  // extract first parameter recursively call self others template <typename u, typename... t> constexpr bool check(u&& f, t&&... t) {     // short-circuit...     return f() && check(std::forward<t>(t)...); }  template <typename t, bool v> struct {   // method can called @ compile time   constexpr bool operator()() const {       return v;   } };  int main() {   constexpr auto v1 = check(a<int, true>{}, a<int, false>{}, a<double, true>{});   static_assert(v1 == false, "expected false!");   constexpr auto v2 = check(a<int, true>{}, a<int, true>{}, a<double, true>{}, a<float, true>{});   static_assert(v2, "expected true!"); } 

i'm sure it's possible simplify further. inheritance stuff not necessary, , computation of flags done @ compile time.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -