×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    template vs. non-template versions of a function

    Hi.

    I have two functions:

    (A):
    TBool MyFunctionX(const TUint16 myInt, const TDesC16& aData)
    {
    RDebug::Printf("normal version of MyFunctionX()\n");
    return EFalse;
    }

    (B):
    template<typename T>
    TBool MyFunctionX(const TUint16 myInt, const T& aData)
    {
    RDebug::Printf("templated version of MyFunctionX() %d \n", sizeof(T));
    return EFalse;
    }

    and variables:

    _LIT16(KDesC16,"16 bit descriptor, length:28");
    TBufC16<28> buf16(KDesC16);

    case 1:

    A call to:

    MyFunctionX(513, buf16);

    calls (B) (which is strange!)
    where compiler generates specialization of the template like this:

    template< TBufC16<28> >
    TBool MyFunctionX(TUint16 myInt, const TBufC16<28>& aData)


    case 2:

    2) A call to:

    MyFunctionX(513, (const TDesC16& )buf16);
    calls (A) (which is correct)


    case 3:

    A call to:
    MyFunctionX<const TDesC&>(513, buf16);
    calls (B) (which is correct)

    Why can I not pass buf16 (on its own with no casting or whatever, as in the first case) as an argument normally and have (A) be called as that would be normal/expected behaviour...?

    Thanks,
    Chi

  2. #2
    Regular Contributor
    Join Date
    Nov 2006
    Location
    Chengdu China
    Posts
    169

    Re: template vs. non-template versions of a function

    If I remember correctly there is one similar case in Java
    void f(Object o){} //1
    void f(int[] i){} //2
    f(null); //will call 2
    Some guy explained that it s because 2 is 'more specific than 1, or 1 is more general than 2', and he says, compiler prefer specific to general.
    So, in this case, if we have two functions with one taking the base and the other taking the derive, I think compiler will choose the latter over the former

    But who made these rules? I do not know
    Any specification to refer to? Again, I have no idea
    The dream is to become one machine, or let one machine become me

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    6

    Re: template vs. non-template versions of a function

    Thanks for that, it is strange behaviour but I guess a work-around will have to be used.

Similar Threads

  1. ShortMsgReceived - Delphi
    By qb_soft in forum PC Suite API and PC Connectivity SDK
    Replies: 9
    Last Post: 2008-06-22, 10:30
  2. Help with lnk2019 lnk1120 error
    By yuenfatt in forum Symbian
    Replies: 8
    Last Post: 2007-08-24, 08:21
  3. C-code..unable to make sis
    By Symbian_Neil in forum Symbian
    Replies: 9
    Last Post: 2006-12-02, 07:55
  4. compile PyS60 error
    By adywicaksono in forum Symbian
    Replies: 2
    Last Post: 2006-05-08, 14:32
  5. ARMI Template error
    By moldovan_catalin in forum Symbian
    Replies: 2
    Last Post: 2005-09-20, 07:39

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×