c++ - Storing a pointer to a template type of the same template class -


let's have following:

template<typename a, typename b> class foo { private:     m_a;     b m_b;     foo<a,b>* m_pfoo;  public:     foo( a, b b, foo<a,b>* pfoo = nullptr ); }; 

with class can save pfoo m_pfoo long instantiation of types match such:

 int main() {      foo<int, int> foo1( 3, 5 );      foo<int, int> foo2( 2, 4, &foo1 ); // works      foo<int, int> foo3( 5, 7, &foo2 ); // still works        foo<double, int> foo4( 2.4, 5 );      foo<double, int> foo5( 7.5, 2, &foo4 ); // works      foo<double, int> foo6( 9.2, 6, &foo5 ); // still works       // compiler error - can not deduce template arguments      foo<double, int> foo7( 3.7, 2, &foo1 ); // doesn't work       return 0;  } 

in previous question demonstrated similar problem , initial question how pass pointer class template type same class template constructor, 1 of responses received regarding passing in pointer not problem, storage is. post new question becomes this:

how able have same class template same or similar constructor above each of these types:

foo<short, short>           ssfoo; foo<short, int>             sifoo; foo<short, int64_t>         si64foo; foo<short, unsigned>        sufoo; foo<short, float>           sffoo; foo<short, double>          sdfoo; foo<short, long>            slfoo; foo<short, long long>       sllfoo;  foo<int, short>             isfoo; foo<int, int>               iifoo; foo<int, int64_t>           ii64foo; foo<int, unsigned>          iufoo; foo<int, float>             iffoo; foo<int, double>            idfoo; foo<int, long>              ilfoo; foo<int, long long>         illfoo;  foo<int64_t, short>        i64sfoo; foo<int64_t, int>          i64ifoo; foo<int64_t, int64_t>      i64i64foo; foo<int64_t, unsigned>     i64ufoo; foo<int64_t, float>        i64ffoo; foo<int64_t, double>       i64dfoo; foo<int64_t, long>         i64lfoo; foo<int64_t, long long>    i64llfoo;  foo<unsigned, short>        usfoo; foo<unsigned, int>          uifoo; foo<unsigned, int64_t>      ui64foo; foo<unsigned, unsigned>     uufoo; foo<unsigned, float>        uffoo; foo<unsigned, double>       udfoo; foo<unsigned, long>         ulfoo; foo<unsigned, long long>    ullfoo;  foo<float, short>           fsfoo; foo<float, int>             fifoo; foo<float, int64_t>         fi64foo; foo<float, unsigned>        fufoo; foo<float, float>           fffoo; foo<float, double>          fdfoo; foo<float, long>            flfoo; foo<float, long long>       fllfoo;  foo<double, short>          dsfoo; foo<double, int>            difoo; foo<double, int64_t>        di64foo; foo<double, unsigned>       dufoo; foo<double, float>          dffoo; foo<double, double>         ddfoo; foo<double, long>           dlfoo; foo<double, long long>      dllfoo;  foo<long, short>            lsfoo; foo<long, int>              lifoo; foo<long, int64_t>          li64foo; foo<long, unsigned>         lufoo; foo<long, float>            lffoo; foo<long, double>           ldfoo; foo<long, long>             llfoo; foo<long, long long>        l_llfoo;  foo<long long, short>       llsfoo; foo<long long, int>         llifoo; foo<long long, int64_t>     lli64foo; foo<long long, unsigned>    llufoo; foo<long long, float>       llffoo; foo<long long, double>      lldfoo; foo<long long, long>        ll_lfoo; foo<long long, long long>   ll_llfoo; 

are valid types store within class template upon construction address of previous instance being passed new instance's constructor? also; how able prevent class accepting custom or user defined object or character, string types, enumerations , boolean types? typenames being passed class templates argument list being numerical types only.

an instance of template distinct type, separate other types, , other instances of template.

foo<short, short> 

and

foo<int, int> 

are 2 different classes, different each other as

foo1; 

and

foo2; 

are different each other.

these different classes. follows that:

foo<short, short> *m_pfoo; 

and

foo<int, int> *m_pfoo; 

are different each other as

foo1 *m_pfoo; 

and

foo2 *m_pfoo; 

are. c++ doesn't work way. m_pfoo member of template can point 1 class. have pick 1 is. same type own class, that's 1 option. or, can point instance of other class. can 1 class.

unless, of course, make a

void *m_pfoo; 

but, you'll lose type safety , type checking, of course, going route.

as it's been mentioned in comments, might able derive template superclass, , store pointer superclass:

class foobase {  // ...  };  template<typename a, typename b> class foo : public foobase {      m_a;     b m_b;     foobase* m_pfoo;  public:     foo( a, b b, foobase* pfoo = nullptr ); }; 

so, you'll able pass pointer foo<a, b> constructor, automatically casted superclass, , pointer superclass stored.

this approach, of course, has many other implications, cleanest, type-safe approach -- @ least point -- can think of.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -