Like Tree1Likes
  • 1 Post By Elkvis

I thought overloading on return type isn't allowed !

This is a discussion on I thought overloading on return type isn't allowed ! within the C++ Programming forums, part of the General Programming Boards category; I thought overloading on return type *wasn't allowed.. but apparently something changes when templates are involved. Consider the following code: ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 01:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21