c++ - Value type of the class complex, or class itself -


i'm trying have class similar following

#include <cstdlib> #include <iostream> #include <typeinfo> #include <type_traits> #include <complex>  template<class k, class t = typename std::conditional<std::is_class<k>::value, typename k::value_type, k>::type> class {     public:         k v;         t u;         void type() {             std::cout << typeid(u).name() << std::endl;         } };  int main() {     a<std::complex<double>> a;     a.type();     a<std::complex<float>> b;     b.type();     a<float> c;     c.type();     a<double> d;     d.type();     return 0; } 

such ouput be:

d f f d 

stated otherwise, need variable u of type t if k of type std::complex<k>, or k otherwise. can achieved c++11 ? thanks.

you can use partial specialization right type, maybe this:

template <typename t, bool> struct valuetype {     using type = t; }; template <typename t> struct valuetype<t, true> {     using type = typename t::value_type; };  template <class k> struct  {     using t = typename valuetype<k, std::is_class<k>::value>::type;      void type()     {         std::cout << typeid(t).name() << std::endl;     } }; 

if want appropriate data member, can make type alias class member, too, , declare data member of type t.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -