c++ - variadic templated Object multiplication -


in below code doing multiplication variadic templates int values , objects. works primitive types. works 2 objects. code doesn't compile when use more 2 arguments objects multiply.

multiply(1, 2, 3, 4, 5, 6) //works correcyly multiply( a(1), b(1))      //works correctly  multiply( a(1), b(1), b(1) );   //compile time error  multiply( a(1), b(1), b(1), b(1) );   //compile time error  

how solve problem more 2 object multiplication? multiplication done left associative.

#include <iostream> #include <assert.h> #include <cstddef> #include <typeinfo> #include <stdlib.h>  using namespace std;  template <typename...> struct mults;  template <typename t1> struct mults<t1> {     typedef t1 type; };  template <typename t1, typename... ts> struct mults<t1, ts...> {     static typename mults < ts...>::type makets(); //a     static t1 maket1(); //b     typedef decltype(maket1() * makets()) type; //c };  template <typename t> t multiply(const t& v) {     return v; }  template <typename t1, typename... ts> auto multiply(const t1& v1, const ts&... rest) -> typename mults<t1,     ts...>::type //instead of decltype {     return v1 * multiply(rest...); }  struct b; struct {     friend operator*(const &, const b &);     friend ostream & operator<<(ostream &os, const &a);     a(int val = 0) : i(val) {}     private:     const int i; }; struct b {     friend operator*(const &a, const b &b) {         return a(a.i * b.i);     }     b(int val = 0) : i(val) {} private:     const int i; };  ostream &operator<<(ostream &os, const &a) {     return os << a.i; }  int main() {     cout << multiply(1, 2, 3, 4, 5, 6) <<endl;//works correcyly     cout << multiply( a(1), b(1))<<endl;      //works correctly      //cout << multiply( a(1), b(1), b(1) );   //compile time error  } 


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -