Explicit Specialization cannot have a storage class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • themadme
    New Member
    • Mar 2007
    • 53

    Explicit Specialization cannot have a storage class

    Hi there,

    I am porting code from windows using visual studio to mac with xcode.

    Quite a lot of issue have a appeared, which is no surprise, one warning that keeps on appearing is Explicit Specialiszation cannot have a storage class:

    Code:
    enum SortReturnCodeEnum {LOWEST=1, HIGHEST=2, HIGHLOW = 3, IDENTICAL=4};
    template<typename tKey>
    static SortReturnCodeEnum DefaultCompare(typename ArgType<tKey>::ARGT Value1, typename ArgType<tKey>::ARGT Value2)
    {
    	if(Value1 < Value2)
    	{
    		return LOWEST;
    	}
    	else if(Value1 > Value2)
    	{
    		return HIGHEST;
    	}
    	return IDENTICAL;
    }
    
    //here it complains about the warning and suggest removing //the 'static' which will fail on visual studio 
    template<>
    static SortReturnCodeEnum DefaultCompare<const char*>(typename ArgType<const char*>::ARGT Value1, typename ArgType<const char*>::ARGT Value2)
    {
    	const char *pOne = Value1;
    	const char *pTwo = Value2;
    
    	while(*pOne && *pTwo)
    	{
    		if(*pOne < *pTwo)
    		{
    			return LOWEST; 
    		}
    		else if(*pOne > *pTwo)
    		{
    			return HIGHEST;
    		}
    		++pOne;
    		++pTwo;
    	}
    
    	if(!(*pOne) && *pTwo)
    	{
    		return HIGHEST;
    	}
    	else if(*pOne && !(*pTwo))
    	{
    		return LOWEST;
    	}
    	return IDENTICAL;
    }
    I could do a #define __GNUC__ but i was checking if anyone has came across this problem.

    thanks
    Last edited by zmbd; Jan 31 '13, 09:06 PM. Reason: [Z{Please use the [CODE/] button to format posted script}]
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You just need to remove the static at line 18. You can not use a storage class there because the storage class of the template is already set at line 3

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Opps just saw you comment at line 16.

      I think the point is that declaring a template static is an unlikely thing to do because of the way that the linking resolves template instantiations.

      That is every C file that uses the template puts a copy of the compiled template into its object file, when the linker sees all those copies of the same functions/methods since it knows they are templates rather than raise an error (as it would if you hard coded a function more than once) it discards all but one of them that it includes in the final executable.

      By this mechanism you automatically end up calling a template function that most likely is in a different file to the one it is defined in, that is the template needs to have external linkage, giving it static linkage can only break this mechanism.

      So in light of that I would say remove the static from line 18 and 3.

      Comment

      • themadme
        New Member
        • Mar 2007
        • 53

        #4
        First thanks zmbd for formatting the code, my bad and thank you very much Banfa for the insight reply.

        I'm trying to remember why I used the static key word in the first place, anyway I removed the static key words as recommended as well then placed the inline keyword for template specialization function to prevent complier errors.

        I think I may need to and read over Bjarne Stroustrup The C++ Programming Language

        Comment

        Working...