c++ - typename when defining map data that is a function pointer with a sprinkling of templates -


This is a strange question because I already know the answer to 'coding'. I just want to understand why this is so. Here are the gurus, who explain these things better than the C ++ standard.)

Below we have a tool to define a such factory template, which is based on a string based on a key (This is a customized example): -

  #include & lt; Iostream & gt; # Include & lt; Map & gt; #include & lt; String & gt; using namespace std; Template & lt; Typename t, typename TProduct & gt; TProduct * MyFactoryConstructHelper (CONST T * T) {If (! T) returns new T; New T Return (* Fixed_cast & lt; ConstT * & gt; (T)); } Template & lt; Typename TProduct & gt; Class AbrishFactory {Public: TypeTouch TProduct * (* MyFactoryConstructor) (Constant Worth *); Typedef Map & lt; String, MyFactoryConstructor & gt; MyFactoryConstructorMap; Constant TProduct * Create (Const String and INM) {MyFactoryConstructor ctr = mTypes [iName]; TProduct * Result = Faucet; If (CTR) result = CTR (faucet); Return result; } Template & lt; Typename T & gt; Static Open Register (CONST STRENGTH & INME) {Type TIFF TProduct * (* ConstructPtr) (CONST * *); ConstructPtr cPtr = MyFactoryConstructHelper & lt; T, TProduct & gt; String name = iName; MTypes.insert (pair  (name, reinterpret_cast & lt; MyFactoryConstructor & gt; (CPTR)); Return (right); } Conserved: Sarfaitcher () {} Fixed MyFactoryConstructorMap mTypes; }; Template & lt; Typename TProduct & gt; Maps & lt; String, / * typename * / AbstractFactory & lt; TProduct & gt; :: MyFactoryConstructor & gt; AbstractFactory & LT; TProduct & gt; :: mTypes; Here's an example of how we use it: -  
  class MyProduct {public: Virtual ~ MyProduct () {} Virtual Zero Iam ( ) = 0; }; Class MyProductFactory: Public Abibrating & lt; MyProduct & gt; { public: }; Category Product A: Public MyProduct {Public: Zero Iam () {cout & lt; & Lt; "Product A" & lt; & Lt; Endl; }}; Category ProductB: Public MyProduct {Public: Zero Iam () {cout & lt; & Lt; "Productby" & lt; & Lt; Endl; }}; Int _tmain (int argc, _TCHAR * argv []) {MyProduct * prd; MyProductFactory :: Register & lt; Productà & gt; ( 'a'); MyProductFactory :: Register & lt; ProductB & gt; ("B"); PRD = MyProductFactory :: Create ("A"); Prd-> Iam (); Remove PDD; Prd = MyProductFactory :: Create ("B"); Prd-> Iam (); Remove PDD; Return 0; }   

This will not compile, complaining that there is not a valid template type argument for the data type in the map. But if you remove comments around the 'typewriter' keyword in the stable member definition, then everything is fine and works ... why?

Even more, can I make it better? :)

The standard one implementation probably tries to parse and identify several errors in a template When it reads the template definition, before any instantance. C ++ references are not independent, however, if you do not know which types of symbols are and how are templates, if the symbol is dependent (depending on the template parameter on some parameters), then you can type the compiler one type Or need to say a template; Otherwise, the compiler should believe that this is something else. In this case, you are saying the compiler that the AbstraFacture :: MyFactoryConstructor is a type of name, and nothing else.

If, when the template is turned on, and the compiler can see what the symbol is actually bound to be, it has come to know that you have lied (eg AbstractFactor :: MyFactoryConstructor is actually an int ), Then the compiler will get you crazy.

Note that the definition of abrasterfactor was defined before nothing changes to the requirement of Typedef can always be a clear expertise for the type on which you are turning on the AbstraFactifier.

Comments