CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    Member templates

    Say I have a template class, with a member template as the following:
    Code:
    template <typename T>
    class CLASS{
    
    template <typename T2>
        void f(T2 const&);
    };
    Is it possible to have a specialization for f(T2 const&) withot specifying T?
    I mean,can I define a special treatment for T2=int no matter what T stands for?
    **** **** **** **** **/**

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Member templates

    Have you tried
    Code:
    template <typename T>
    void CLASS<T>::f2<int>(int const&)
    {
    	// ...
    }
    ?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    Re: Member templates

    The truth is that member is an operator , and by looking at the compiler's
    error I assume the syntax is different.

    I get the following compiler errors:
    "`operator+' used as template "
    "use `CLASS<T>::template operator+' to indicate that it is a template "
    "expected init-declarator at end of input "

    I've tried to use the following :
    Code:
    CLASS<T> CLASS<T>::operator+<double>(const double &d){
                    std::cout << "2" << std::endl;
                    return *this;}
    OR

    Code:
    CLASS<T> CLASS<T>::template operator+<double>(const double &d){
                    std::cout << "2" << std::endl;
                    return *this;}
    but its not working.
    **** **** **** **** **/**

  4. #4
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Member templates

    This works for me, in Dev Studio 7.x:
    Code:
    template<typename _T>
    class TempStruct
    {
        TempStruct<_T> operator+(const double &d1);
    
        int m_nI;
    };
    
    template<typename _T>
    TempStruct<_T> TempStruct<_T>::operator+(const double &d1)
        {
            std::cout << "2" << std::endl;
            return *this;
        }
    As an aside, don't you want to return a reference to the class?

    Viggy

  5. #5
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    Re: Member templates

    I wish it was that simple. My first general code shows two levels
    for template parameters,
    T for the class, and T2 for a function member:

    Code:
    template <typename T>
    class CLASS{
    
    template <typename T2>
        void f(T2 const&);
    };
    when it comes to operator, I can't get it work.
    (gcc 3.4.2 on Dev-C++ 4.9.9.2)

    =============
    Edit:
    Indeed the return type should be a reference,
    but my concern is on templates practice
    Last edited by Guysl; September 27th, 2005 at 04:38 PM.
    **** **** **** **** **/**

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Member templates

    Ahh, I missed that. Yeah, that's an interesting one.

    Hmmm, Stroustrup doesn't say anything about specializing a template member function of a template class.

    Viggy

  7. #7
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Member templates

    I suggest something similar to what Graham has given. Here is something I think should work but not on VC++ 6.0 -, may be on VS 2003 (I shall provide a link that tells this). I was not able to test this because I have VC++ 6.0 and I am a bit confused about what Comeau documentation has to say about this. First the code that I think should work.
    Code:
    #include <iostream>
    
    template <typename T>
    class CLASS{
    public:
    	template<typename T2>
    	void f(const T2&);
            void f(const int&);
    };
    
    template<typename T>
    template<typename T2>
    void CLASS<T>::f(const T2&){std::cout << "template function called\n";}
    	
    template<typename T>
    void CLASS<T>::f(const int&){std::cout << "overload/specialization called\n";}
    
    int main(){
    	int i = 10;
    	double d = 10.001;
    	CLASS<double> obj;
    	obj.f(i);
    	obj.f<double>(d);
    	return 0;
    }
    This code compiles fine with Comeau. Let's see if this works on the compiler that you are using. Now here are some links that might help you:
    1. The comeau documentation - here - Comeau Template FAQ - How to define a member function outside of its template?
    2. The MSDN documentation - Member function Templates

    See if this helps - and if not please mention the errors and provide the exact compilable code that you are using. You gave us a small example and then you said it is more than that so that keeps people just guessing.

    //EDIT: I tested the above quoted code on gcc and it works fine for me. Hope this helps (provided this is what you were looking for).
    Last edited by exterminator; September 28th, 2005 at 01:43 AM.

  8. #8
    Join Date
    Sep 2004
    Posts
    519

    Re: Member templates

    One thing to note is that template specializations for methods don't overload (http://www.gotw.ca/gotw/049.htm).

    Also, I agree with exterminator. You should provide the forums with source code that encapsulates the whole problem. Whether it's your original source code or an example illustrating the problem. It gets frustrating to invest time in an answer to the original question only to get the response: "Yeah, but...".

    Anyway, I think exterminators suggestion by using regular overload is probably closer to what you want (esp. considering that specializations don't overload). Provide a regular non-template member function that overloads the template method. The compiler will choose the non-template function if it's an exact match.

    Hope this helps

  9. #9
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    Re: Member templates

    Thanks alot guys,
    I've used this information to suggest a solution for :
    http://www.codeguru.com/forum/showth...06#post1241906
    **** **** **** **** **/**

  10. #10
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: Member templates

    VC7 got all of them right according to Sutter. I got 11/12. If I reproduce it here with my code, ask people what they think the output will be.

    Code:
    #include <iostream>
    #include <complex>
    template<typename T> void writeln( const T& t ) { std::cout << t << std::endl; }
    
    template<typename T1, typename T2>
    void f( T1, T2 ) { writeln( 1 ); }                       // 1
    template<typename T> void f( T ) { writeln( 2 ); }       // 2
    template<typename T> void f( T, T ) { writeln( 3 ); }    // 3
    template<typename T> void f( T* ){ writeln( 4 ); }      // 4
    template<typename T> void f( T*, T ) { writeln( 5 ); }   // 5
    template<typename T> void f( T, T* ) { writeln( 6 ); }   // 6
    template<typename T> void f( int, T* ) { writeln( 7 ); } // 7
    template<> void f<int>( int ) { writeln( 8 ); }          // 8
    void f( int, double ) { writeln( 9 ); }                  // 9
    void f( int ) { writeln( 10 ); }                          // 10
        
    int main()
    {
       int             i=0;
        double          d=0.0;
        float           ff=0.0f;
        std::complex<double> c;
    
        f( i );         // a
        f<int>( i );    // b
        f( i, i );      // c
        f( c );         // d
        f( i, ff );     // e
        f( i, d );      // f
        f( c, &c );     // g
        f( i, &d );     // h
        f( &d, d );     // i
        f( &d );        // j
        f( d, &i );     // k
        f( &i, &i );    // l
    }

  11. #11
    Join Date
    Jun 2003
    Location
    not-so-Great Britain
    Posts
    178

    Re: Member templates

    a) 10
    b) 8
    c) 3
    d) 2
    e) 1
    f) 9
    g) 6
    h) 7
    i) 5
    j) 4
    k) 1
    l) 3

    I think.
    Hungarian notation is the bane of self documenting code.
    Forget all fancy tricks with operator precedence. Code should be easily readable.
    May the BOOST be with you.
    Good free E-Books thanks to Bruce Eckel.

  12. #12
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Member templates

    Quote Originally Posted by marten_range
    One thing to note is that template specializations for methods don't overload (http://www.gotw.ca/gotw/049.htm).
    I had deferred reading this article because I was quite busy these days and still am, now I am through with it though a bit late. The things there fit in quite well and logically. But I still was not able to figure out what you meant when you said that template specializations for methods don't overload? I saw in that article that the correct functions (or template instantiations) were called depending upon the best match (and the rules set up by the standard) and hence I would conclude that overloading is taking place. I would be needing an explanation for the words you used because I am finding them a bit confusing to me. Hoping for a response. Regards.

  13. #13
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up Re: Member templates

    Were you referring to something like - the rules for calling the overloads of function templates (their instantiations), their specializations and normal overload forms (for those functions themselves) are different from that of normal function overloading?

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