Prev: I keep running into long term c++ programmers who refuse to use exception
Next: display integer in MessageBox
From: Thomas Petit on 29 Mar 2010 06:21 On 15 mar, 13:35, PGK <graham.k...(a)gmail.com> wrote: > In the class "Foo" below, is it possible to use a constructor > initialization list to initialize "cs", a tuple of complex values? > > // #include <iostream>,<tuple>,<complex>,<type_traits> > > template <typename T, typename ... Ts> > struct Foo { > std::tuple<std::complex<T>, std::complex<Ts>...> cs; > > template<typename ... Us> > Foo(T c1, T c2, Us ... ts) : ???? { } > > }; > > int main(int argc, char *argv[]) { > Foo<int,bool,double> a(1,2,true,false,42.0,43.0); // For example > > } Phew, what a great brain teaser ! I found an another way : template <typename T> std::tuple<std::complex<T>> make_tuple_complex(T real, T im) { return std::make_tuple(std::complex<T>(real, im)); } template <typename Head, typename... Tail> auto make_tuple_complex(Head real, Head im, Tail... tail) -> decltype(std::tuple_cat( make_tuple_complex(real, im), make_tuple_complex(tail...))) { return std::tuple_cat( make_tuple_complex(real, im), make_tuple_complex(tail...)); } template <typename... Ts> struct Foo { std::tuple<std::complex<Ts>...> cs; template <typename... Us> Foo(Us... us) : cs(make_tuple_complex(us...)) {} }; int main() { Foo<int, double> foo(4, 5, 1.0, 4.0); } -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: PGK on 31 Mar 2010 07:34 > Phew, what a great brain teaser ! > I found an another way : > > template <typename T> > std::tuple<std::complex<T>> > make_tuple_complex(T real, T im) > { > return std::make_tuple(std::complex<T>(real, im)); > > } > > template <typename Head, typename... Tail> > auto make_tuple_complex(Head real, Head im, Tail... tail) > -> decltype(std::tuple_cat( make_tuple_complex(real, im), > make_tuple_complex(tail...))) > { > return std::tuple_cat( make_tuple_complex(real, im), > make_tuple_complex(tail...)); > > } > > template <typename... Ts> > struct Foo > { > std::tuple<std::complex<Ts>...> cs; > > template <typename... Us> > Foo(Us... us) : cs(make_tuple_complex(us...)) {} > > }; > > int main() > { > Foo<int, double> foo(4, 5, 1.0, 4.0); > > } Glad you're enjoying the puzzle; me too. Unfortunately your approach doesn't work with more than 4 arguments. (I think you need a one-to-one correspondence between template and normal arguments(?)) Shame, it would be nice if it could be shortened. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Pages: 1 2 Prev: I keep running into long term c++ programmers who refuse to use exception Next: display integer in MessageBox |