CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    326

    conversion operator in template

    Hi,

    Brief description:
    ---------------------
    I have a conversion operator that seems to work ok when used without templates.
    But the conversion operator doesn't seem to be working when used with templates.

    Example (complete code is given below):
    ------------------------------------------------
    I have 2 template classes, ClassA<T> and ClassB<T>

    ClassB has a conversion operator to convert ClassB to "const ClassA<T>* const"

    Function f1 - without template
    ---------------------------------------
    Code:
    void f1(const ClassA<int>* const pPtrY).
    When this function f1 is invoked by passing an object of ClassB<int>, the conversion operator gets invoked, and it is converted to "const ClassA<int>* const".

    This works as expected

    Function f2 - with template
    ----------------------------------
    Code:
    template<typename T>
    void f2(const ClassA<T>* const pPtrY)
    When this function f2 is invoked by passing an object of ClassB<T>, the conversion operator doesn't get invoked and hence throws an error.


    Doubt
    --------
    Why does the conversion operator not work with templates ?
    What is the underlying concept ?
    Or am I missing something ?

    complete code given below:
    Code:
    #include<iostream>
    
    template<typename T>
    class ClassA
    {};
    
    template<typename T>
    class ClassB
    {
        public:
            ClassB();
            ClassB(ClassA<T>* const pPtrX);       //Copy Constructor
            operator const ClassA<T>* const () const;   //Conversion Operator
    
        private:
            ClassA<T> *ptrX;
    
    };
    
    void f1(const ClassA<int>* const pPtrY)
    {}
    
    template<typename T>
    void f2(const ClassA<T>* const pPtrY)
    {}
    
    int main()
    {
        
        ClassA<int> *p1 = new ClassA<int>;
        ClassB<int> b1(p1);
        
        f1(b1);    //works ok, when without a template
                   //conversion operator is invoked to convert b1 to ClassA<T>*
        
        //f2(b1);  //throws an error, when with a template
                   //conversion operator is not invoked, can't understand why
                   //what is the reason and what is the undelying concept ?
        return(0);
    }
    
    template<typename T>
    ClassB<T> :: ClassB()
        : ptrX(NULL)
    {}
    
    template<typename T>
    ClassB<T> :: ClassB(ClassA<T>* const pPtrX)
        : ptrX(pPtrX)
    {}
    
    template<typename T>
    ClassB<T> :: operator const ClassA<T>* const () const
    {
        return(ptrX);
    }
    Last edited by Muthuveerappan; July 13th, 2009 at 01:06 AM.

  2. #2
    Join Date
    Aug 2008
    Posts
    18

    Re: conversion operator in template

    Hello
    Make this correction in your code
    f2<int>(b1);

    it will work

    i copied ur code and corrected
    its working ..

  3. #3
    Join Date
    Nov 2006
    Posts
    1,611

    Re: conversion operator in template

    srishi1302 is correct, but if that's not quite what you want, here's a helper approach.

    It appears that it's too much to ask the compiler to automatically determine T from ClassB<T>. It's not willing to reach inside ClassB to get the T out of it, and supply that to T for automatic generation of ClassA<T> types for the template function, but you can help it to know what you really mean.

    try

    Code:
    template<typename T>
    void f2i(const ClassA<T> * const pPtry)
    {
    }
    
    
    template<typename T>
    void f2(ClassB<T> & b )
    {
     const ClassA<T> * const x = b;
    
     f2i( x );
    }
    The idea is to provide an interface for f2i which peals out the appropriate T for it to "know" you mean ClassA<T>, transporting the knowledge of T (interior to ClassB's type) to the call for f2i.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

  4. #4
    Join Date
    Feb 2009
    Posts
    326

    Re: conversion operator in template

    Thanks a lot srishi1302 and Jvene

    I think Jvene's solution is the best we have.


    The following I guess is beyond my reach, but just thought I would state it:

    I think (I might be completely wrong), the way variable declarations are parsed might be different from the way function calls are parsed, the reason I say that the following is accepted by the compiler:

    (Variable declaration from Jvene's example - this works)
    Code:
    const ClassA<T> * const x = b;
    (Function call which throws an error)
    Code:
    f2(b1);

  5. #5
    Join Date
    Nov 2006
    Posts
    1,611

    Re: conversion operator in template

    This

    const ClassA<T> * const x = b;

    In the context of the template function already deduced T, so it was conveyed "outside" of ClassB<T>, where as in

    f2(b1);

    T is "inside" ClassB, and isn't conveyed as the T for ClassA<T>

    ...at least that's a colloquial way of describing it.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured