c++ - "Ambiguous template specialization" problem -


I am currently stacking code which has already been compiled with Visual Studio 2008. This type of code in this code:

  template & lt; Typename T & gt; T convert (const char * s) {// slow catch-all std :: istringstream (s); T writ; Is & gt; & Gt; Retired; Return writ; } Template & lt; Typename t, typename t2 & gt; T Convert (T2 * S) {Return Conversion & lt; T & gt; (Static_cast & lt; const char * & gt; (S)); } Template & lt; Typename t, typename t2 & gt; T convert (T2S) {Return T (S); } Template & lt; & Gt; Inline ant convert & lt; Int & gt; (Const char * s) {return (int) atoi (s); }  

Generally, there are many types of template functions with different types of return types:

  int i = convert & lt; Int & gt; (SIIANT); The problem is, the result of these template specializations is in "Unclear template specialization" if it was anything other than the return type which differentiates these functions specializations, I can use overload, but this is not an option.  

Called Convert Functions?

Update I have added these two catch-all template experts, which I left around for the first time. I'm embarrassed to say, I am unsure about inspiration for another, but firstly, due to a convert function, it is used in many places where string data is passed as zero *. I can not see it with the GCC yet, but I suspect that this may be a problem.

Update 2 Here is the full CPP file that will reproduce this. If you exclude both "general purpose" functions, then it will compile if you allow them to remain either, then vague template expertise will result in error.

  #include & lt; Iostream & gt; # Include & lt; Sstream & gt; Template & lt; Typename T & gt; T convert (const char * s) {// This is a slow general purpose, if no specialization is provided std :: istringstream; T writ; Is & gt; & Gt; Retired; Return writ; } // General Purpose 1 Template & lt; Typename t, typename t2 & gt; T Convert (T2 * S) {Return Conversion & lt; T & gt; (Static_cast & lt; const char * & gt; (S)); } // General Purpose 2 template & lt; Typename t, typename t2 & gt; T convert (T2S) {Return T (S); } // Type special template & lt; & Gt; Inline Float Convert & lt; Float & gt; (Const char * s) {return (float) atof (s); } Int main (int argc, const char * sz []) {return 0;  

apparently change back & lt; T & gt; (Static_cast & lt; const char * & gt; (s)); (or something else which I can not see) is encouraging the compiler to create a template installation of T Convert (Const char * s) for T = float, then when you If you try to do this later, it fails because the template version already exists.

When I inline float lieutenant; & Lt; Float & gt; (Const char * s) General purpose converters (immediately after the const char * template function), I was able to successfully compile with g ++ 4.2.


Comments