CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2009
    Posts
    58

    operator++ with templates

    Code:
    template <class T>
    T operator++(T num)
    {
         cout<<"In overload function"<<endl;
    	 return T result = num++;
    }
    
    int main()
    {
       int num=5;
       int result = operator++(num); //whats the problem with this???
    
    }


    Why does this fail to compile?

  2. #2
    Join Date
    Mar 2009
    Location
    Bulgaria
    Posts
    63

    Re: operator++ with templates

    Because you're trying to declare a variable inside a return statement.
    And by the way... I'd think for a minute before committing/submitting this... whatever it does.

  3. #3
    Join Date
    Mar 2009
    Posts
    58

    Re: operator++ with templates

    The return statement is not the problem here.

    I am visual studio 2008 if thats any help.

  4. #4
    Join Date
    Mar 2009
    Location
    Bulgaria
    Posts
    63

    Re: operator++ with templates

    Oh, really?! Then you tell me why this fails to compile:
    Code:
    int main()
    {
    	return int i = 0;
    }

  5. #5
    Join Date
    Mar 2009
    Posts
    58

    Re: operator++ with templates

    No need to be smart, what i was saying was even when i change the return part around it still fails. The error i get is before it reaches the return part.

    To make it clearer for you


    Code:
    template <class T>
    T operator++(T num)
    {
         cout<<"In overload function"<<endl;
    	  T result = num++;
                      return result
    }
    
    int main()
    {
       int num=5;
       int result = operator++(num); //whats the problem with this???
    
    }

    This still fails.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: operator++ with templates

    Quote Originally Posted by newbie30
    whats the problem with this?
    For one thing, you cannot overload operator++ for int.

    Basically, it looks like you are trying to implement postfix operator++ in terms of prefix operator++, which is weird: the opposite is more normal. Nonetheless, if you want an example:
    Code:
    #include <iostream>
    
    class X
    {
    public:
        X(int num) : num(num) {}
    
        int get() const
        {
            return num;
        }
    
        X operator++(int)
        {
            X temp(*this);
            ++num;
            return temp;
        }
    private:
        int num;
    };
    
    template <class T>
    T& operator++(T& x)
    {
        x++;
        return x;
    }
    
    int main()
    {
        X x(5);
        X result = operator++(x);
        std::cout << x.get() << ' ' << result.get() << std::endl;
    }
    Personally, I feel that a more useful thing would be to define postfix operator++ in terms of prefix operator++, e.g.,
    Code:
    template<class T>
    T operator++(T& x, int)
    {
        T temp(x);
        ++x;
        return temp;
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Apr 2004
    Posts
    102

    Re: operator++ with templates

    Quote Originally Posted by newbie30 View Post
    No need to be smart, what i was saying was even when i change the return part around it still fails. The error i get is before it reaches the return part.

    To make it clearer for you


    Code:
    template <class T>
    T operator++(T num)
    {
         cout<<"In overload function"<<endl;
    	  T result = num++;
                      return result
    }

    This still fails.
    You are running into a circular definition problem. You have num defined as type T and operator++ for type T is using operator++ for type T inside itself.

    You also missed the semi-colon on your return line.

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: operator++ with templates

    Quote Originally Posted by jefranki
    You are running into a circular definition problem. You have num defined as type T and operator++ for type T is using operator++ for type T inside itself.
    No, notice that postfix operator++ is used in the implementation of the function template prefix operator++. Incidentally, note that my example also corrects the function signature and return type.
    Last edited by laserlight; March 26th, 2009 at 12:43 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Apr 2004
    Posts
    102

    Re: operator++ with templates

    Quote Originally Posted by laserlight View Post
    No, notice that postfix operator++ is used in the implementation of the function template prefix operator++. Incidentally, note that my example also corrects the function signature and return type.
    I opened the editor before your post came up. At any rate, I missed the prefix vs. postfix element.

  10. #10
    Join Date
    Mar 2009
    Posts
    51

    Re: operator++ with templates

    Quote Originally Posted by newbie30 View Post
    Why does this fail to compile?
    What does the compiler say?

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