c++ - Selecting an explicit specialization of a class based on a derived type -


Hello I have a clear expertise in choosing the right version of a templated class which has a clear expertise. I want to select an expertise using the derived class of the class used for expertise. The scenario is:

  #include & lt; Stdio.h & gt; class {}; Class B: Public A {}; Template & lt; Typename T & gt; Class Fu {public: int october (zero) {return 10; }}; // Clear expertise for a template & lt; & Gt; Int Foo & Lt; A & gt; :: FooBar (zero) {returns 20; } Zero main (zero) {Foo < B & gt; FooB; // It prints 10 instead of the desired 20 viz compiler, the normal version selects printf ("% d", fooB.FooBar ()); }  

As I said in my comments, I want to print 20 because B is obtained from A, but instead 10 gets printed. How do I get expertise while using expertise for without each and every derived class ( very derived types in my actual scenario) Have to go

--- Edit: Let's get the new answer, we make the original approach more appropriate. All the important choices can be found in the definition of Fu, it is considered easy to maintain.

  #include & lt; Boost / mpl / if.hpp & gt; # Include & lt; Boost / type_traits / is_base_of.hpp & gt; # Include & lt; Iostream & gt; class {}; Class B: Public A {}; Square c {}; Class D: Public C {}; Class E {}; Struct Default {Static Ent Fobar () {Return 10; }}; Struct Method1 {static int fooBar () {20 returns}; }}; Struct Method2 {static int fooBar () {return 30; }}; Template & lt; Typename t, titiname base class, typewrite, choice 1, typing other Choice & gt; Struct from IfDerives: Promotion :: Emp: :: if_ & lt; Typename boost :: is_base_of & lt; Base Class, T & G; :: Type, Choice 1, Other Choice & gt; :: type {}; Template & lt; Typename T & gt; Struct Foo: IfDerives & lt; T, A, method 1, ifDerives & lt; T, C, Method2, DefaultMethod & gt; & Gt; {}; Int main () {std :: cout & lt; & Lt; Foo & lt; A & gt; :: fooBar () & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; Foo & lt; B & gt; :: fooBar () & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; Foo & lt; C & gt; :: fooBar () & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; Foo & lt; D & gt; :: October () & lt; & Lt; Std :: endl; Std :: cout & lt; & Lt; Foo & lt; E & gt; :: fooBar () & lt; & Lt; Std :: endl; Return 0; }  

--- Original Answer If you can use Boost, you can do something like:

  #include & lt ; Boost / type_traits / is_base_of .hpp & gt; Template & lt; Bool b & gt; Square FooHelper {int FooBar (); }; The template's & lt; & Gt; FooHelper & lt; True> :: FooBar () {return 20;} template & lt; & Gt; FooHelper & lt; False & gt; :: FooBar () {return 10;} template & lt; Typename T & gt; Class Fu {Public: Int Fobar (zero) {Return fuhelter & lieutenant; Boost :: ISbase_off & lt; A, T & gt; :: Type :: Price & gt; (); }};  

Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -