Template specialization not working...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob

    Template specialization not working...

    I have a template function that works for 99% of the cases and want a
    different one for the 1%. The problem is that the template case is too
    "vague" and thus the compiler tries to use it for everything. AND the
    template expects the same type put in as gotten out and the
    specialization puts in a diff. type than it returns.

    Code:
    template <typename TT get(T a)
    {
    ...
    }

    //The one case I want diff.
    std::string get(SomeClass a)
    {
    ...
    }

    int main()
    {
    std::string name( "Jeff" );

    //This is correct
    std::string val = get( name );

    SomeClass a;

    //Won't compile, says:
    //cannot convert 'SomeClass' to 'std::string' in initialization
    std::string val = get( a );
    }

    However, if I try template specialization, it just tells me that the
    specializing template does not match any template declaration.

    CODE:

    //Attempting specialization, but says there's no matching declaring
    template
    template<std::s tring get(SomeClass a)
    {
    ...
    }

    I think it's because the original template declaration takes and
    returns the same type and the specialization takes and returns
    different types. Any ideas?
  • Ian Collins

    #2
    Re: Template specialization not working...

    Rob wrote:
    I have a template function that works for 99% of the cases and want a
    different one for the 1%. The problem is that the template case is too
    "vague" and thus the compiler tries to use it for everything. AND the
    template expects the same type put in as gotten out and the
    specialization puts in a diff. type than it returns.
    >
    Code:
    template <typename TT get(T a)
    {
    ...
    }
    >
    //The one case I want diff.
    std::string get(SomeClass a)
    {
    ...
    }
    >
    int main()
    {
    std::string name( "Jeff" );
    >
    //This is correct
    std::string val = get( name );
    >
    SomeClass a;
    >
    //Won't compile, says:
    //cannot convert 'SomeClass' to 'std::string' in initialization
    The template should match here.
    std::string val = get( a );
    }
    >
    However, if I try template specialization, it just tells me that the
    specializing template does not match any template declaration.
    >
    CODE:
    >
    //Attempting specialization, but says there's no matching declaring
    template
    template<std::s tring get(SomeClass a)
    {
    ...
    }
    >
    That isn't a specialisation, the return type differs from the parameter
    type.

    --
    Ian Collins.

    Comment

    • kwikius

      #3
      Re: Template specialization not working...


      ----- Original Message -----
      From: "Rob" <someidunknown1 234@yahoo.com>
      Newsgroups: comp.lang.c++
      Sent: Tuesday, April 29, 2008 1:22 AM
      Subject: Template specialization not working...

      >I have a template function that works for 99% of the cases and want a
      different one for the 1%. The problem is that the template case is too
      "vague" and thus the compiler tries to use it for everything. AND the
      template expects the same type put in as gotten out and the
      specialization puts in a diff. type than it returns.
      <...>
      I think it's because the original template declaration takes and
      returns the same type and the specialization takes and returns
      different types. Any ideas?
      This works for me ...

      #include <string>
      template <typename TT get(T a)

      {

      return a;

      }

      struct SomeClass{};

      //The one case I want diff.

      std::string get(SomeClass a);

      int main()

      {

      std::string name( "Jeff" );

      //This is correct

      std::string val = get( name );

      SomeClass a;

      std::string val1 = get( a );

      }


      Comment

      • James Kanze

        #4
        Re: Template specialization not working...

        On Apr 29, 2:22 am, Rob <someidunknown1 ...@yahoo.comwr ote:
        I have a template function that works for 99% of the cases and want a
        different one for the 1%. The problem is that the template case is too
        "vague" and thus the compiler tries to use it for everything. AND the
        template expects the same type put in as gotten out and the
        specialization puts in a diff. type than it returns.
        Code:
        template <typename TT get(T a)
        {
        ...
        }
        //The one case I want diff.
        std::string get(SomeClass a)
        {
        ...
        }
        int main()
        {
        std::string name( "Jeff" );
        //This is correct
        std::string val = get( name );
        SomeClass a;
        //Won't compile, says:
        //cannot convert 'SomeClass' to 'std::string' in initialization
        std::string val = get( a );
        This line won't compiler because val is already defined.
        Changing it to:
        val = get( a ) ;
        and it compiles with the three compilers I have access to (g++,
        Sun CC and VC++), calling the explicite overload in each case
        (as it should).
        }
        However, if I try template specialization, it just tells me that the
        specializing template does not match any template declaration.
        CODE:
        //Attempting specialization, but says there's no matching declaring
        template
        template<std::s tring get(SomeClass a)
        {
        ...
        }
        Yes, because you're not specializing the same function. But it
        doesn't matter---usually, you don't specialize template
        functions, but simply provide an overload (as you originally
        did).

        --
        James Kanze (GABI Software) email:james.kan ze@gmail.com
        Conseils en informatique orientée objet/
        Beratung in objektorientier ter Datenverarbeitu ng
        9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

        Comment

        Working...