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

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    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 08:26 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    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
    *nullptr
    Posts
    2,657
    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, 03:28 PM
  2. constructors not allowed a return type
    By kotoko in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2009, 10:51 AM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09: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