Thread: I thought overloading on return type isn't allowed !

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Chennai, India
    Posts
    2,645

    I thought overloading on return type isn't allowed !

    I thought overloading on return type *wasn't allowed.. but apparently something changes when templates are involved.

    Consider the following code:
    Code:
    #include <termios.h>
    #include <unistd.h>
    #include <functional>
    #include <string>
    #include <utility>
    #include <iostream>
    
    class NonCanInput
    {
    public:
        template <typename F>
        typename std::result_of<F()>::type
        operator()(F arbitrary)
        {
            TurnOn t;
            return arbitrary();
        }
        
        int operator()(std::function<int(void)> arbitrary= [](){return std::cin.get();} )
        {
            TurnOn t;
            return arbitrary();
        }
        //overload >> if required
    private:
        class TurnOn
        {
        public:
            TurnOn()
            {
                tcgetattr( STDIN_FILENO, &oldt );
                newt = oldt;
                newt.c_lflag &= ~( ICANON | ECHO );
                tcsetattr( STDIN_FILENO, TCSANOW, &newt );
            }
            ~TurnOn()
            {
                tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
            }
        private:
            struct termios oldt, newt;
            
        };
    };
    The above code works fine.
    I thought a template<> would be needed at the bolded part, but that shows an error called "explicit specialization in non-namespace scope".
    Removing the 'template <>' works fine....and I am not not sure why (as I intended that to be a specialzation of the above) .
    Any explanation?
    Last edited by manasij7479; 10-30-2012 at 09:26 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,343
    your return type may be different, but so is the signature of the function, and that's what you're allowed to overload.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Chennai, India
    Posts
    2,645
    Quote Originally Posted by Elkvis View Post
    your return type may be different, but so is the signature of the function, and that's what you're allowed to overload.
    Right !
    Surprisingly, I did not know that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error: Incomplete type is not allowed?
    By Swadesh in forum C++ Programming
    Replies: 4
    Last Post: 10-27-2012, 04:28 PM
  2. constructors not allowed a return type
    By kotoko in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2009, 11:51 AM
  3. Replies: 6
    Last Post: 04-09-2006, 05:32 PM
  4. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 10:52 PM
  5. Constructors now allowed a return type: Mystery Error
    By wbeasl in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2004, 02:33 PM
Website Security Test