How to do explicit specialization of non-type template function

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

    How to do explicit specialization of non-type template function

    Hi,
    I have a quetion how to specialization a non-type templat function, I
    use VC++ 2005.

    My code is:

    #include "stdafx.h"

    enum VariableType
    {
    enumASCII = 1,
    enumDOUBLE = 2,
    enumDURATION = 3,
    enumINTEGER = 4
    };

    template< VariableType >
    struct HrtTypes{
    };

    template< size_t _size >
    struct INTEGERS
    {
    typedef signed char value_type;
    typedef signed char* pointer;
    typedef HrtTypes<enumIN TEGERTypeTrait;
    enum { size = _size };
    };

    template<typena me T>
    bool foo(T*, typename T::TypeTrait*);

    //specializaton for INTEGERS<1>,<2> , ...<n>
    template<typena me T>
    bool foo(T*, HrtTypes<enumIN TEGER>*)
    {
    return true;
    }

    int main()
    {
    INTEGERS<1>* p1;

    foo(p1, INTEGERS<1>::Ty peTrait*(0));

    size_t n = 4;
    INTEGERS<4p2;

    foo(p2, INTEGERS<4>::Ty peTrait*(0));
    }

    I hope all INTEGERS<1>...t ype can use the specilization for template
    function foo(....). I think the compiler could get that
    HrtTypes<enumIN TEGERis same with INTEGERS<1>::Ty peTrait, so foo(p1,
    INTEGERS<1>::Ty peTrait*(0)) and foo(p2, INTEGERS<4>::Ty peTrait*(0));
    can call the same function above.
    but it seems these not works.

    who can give me more comments
    Thanks very well.

  • Victor Bazarov

    #2
    Re: How to do explicit specialization of non-type template function

    Bill wrote:
    I have a quetion how to specialization a non-type templat function, I
    use VC++ 2005.
    >
    My code is:
    >
    #include "stdafx.h"
    That's not a standard header. You might be better off leaning the
    language without the use of precompiled headers, BTW.
    >
    enum VariableType
    {
    enumASCII = 1,
    enumDOUBLE = 2,
    enumDURATION = 3,
    enumINTEGER = 4
    };
    >
    template< VariableType >
    struct HrtTypes{
    };
    >
    template< size_t _size >
    struct INTEGERS
    {
    typedef signed char value_type;
    typedef signed char* pointer;
    typedef HrtTypes<enumIN TEGERTypeTrait;
    enum { size = _size };
    };
    >
    template<typena me T>
    bool foo(T*, typename T::TypeTrait*);
    >
    //specializaton for INTEGERS<1>,<2> , ...<n>
    template<typena me T>
    bool foo(T*, HrtTypes<enumIN TEGER>*)
    {
    return true;
    }
    [..]
    Sorry, you can't do that. You're trying to define a partial
    specialisation of a function template, which is not allowed.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Pete Becker

      #3
      Re: How to do explicit specialization of non-type template function

      On 2008-02-19 11:10:13 -0500, "Victor Bazarov" <v.Abazarov@com Acast.netsaid:
      Bill wrote:
      >I have a quetion how to specialization a non-type templat function, I
      >use VC++ 2005.
      >>
      >My code is:
      >>
      >#include "stdafx.h"
      >
      That's not a standard header. You might be better off leaning the
      language without the use of precompiled headers, BTW.
      And, especially, without Microsoft's sledge hammer notion of
      precompiled headers.

      --
      Pete
      Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
      Standard C++ Library Extensions: a Tutorial and Reference
      (www.petebecker.com/tr1book)

      Comment

      • Bill

        #4
        Re: How to do explicit specialization of non-type template function

        Thanks for your answer,

        1. Yes, you are right, I agree with remove the stdafx.h from header
        file.
        2. I am still not clear why I can't use particial specialization here,
        doe's vc2005 not support it or the particial specialization not work
        in this case?


        Comment

        Working...