Thread: What exactly is a function signature?

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    What exactly is a function signature?

    Looking around on Google for a definitive definition of function signatures, it seems that some people say one thing and some say another.

    Some say it's everything in the prototype, others say it's just the name and the argument types and names. So who is right? Is the return type part of the signature or not?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    When all else fails, ask ISO.
    Quote Originally Posted by The C++ Standard
    1.3.10 signature [defns.signature]
    the information about a function that participates in overload resolution (13.3): the types of its parameters and, if the function is a class member, the cv- qualifiers (if any) on the function itself and the class in which the member function is declared.2) The signature of a function template specialization includes the types of its template arguments (14.5.5.1).
    Footnote 2 says: "2) Function signatures do not include return type, because that does not participate in overload resolution."

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    OK, so I can overload a function by defining one with the same signature but a different return type.

    Another related question (to avoid starting a new thread): If I have a base class with two overloaded functions and I have a derived class which redefines one of those functions, why then is the second overloaded function unavailable to the derived class?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It could be based on how it's derived, but I am fairly certain that overloading and overriding are mutually exclusive. That is to say Base::Foo (overloaded) is never invoked by calls to Derived::Foo (overriden) because the class it is a member of is part of the signature. So be very explicit when you mean one of the Base::Foos.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Sharke View Post
    OK, so I can overload a function by defining one with the same signature but a different return type.

    Another related question (to avoid starting a new thread): If I have a base class with two overloaded functions and I have a derived class which redefines one of those functions, why then is the second overloaded function unavailable to the derived class?
    Read it again: return types do not play a part in overload resolution. Two different functions must differ in something other than return type. (Unless you don't mean what I seem to think you mean, which is possible.) But the class name does play a part.

  6. #6
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by tabstop View Post
    Read it again: return types do not play a part in overload resolution. Two different functions must differ in something other than return type. (Unless you don't mean what I seem to think you mean, which is possible.)
    I think that's actually the case! What I meant was that I can overload a function with another that has a different return type, not that changing the return type is actually necessary for the overload. I guess it was just a clumsy way of rephrasing the fact that the return type has nothing to do with it.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sharke
    If I have a base class with two overloaded functions and I have a derived class which redefines one of those functions, why then is the second overloaded function unavailable to the derived class?
    Because member names in the derived class hide corresponding member names in the base class, so you should use a using declaration to bring back the member names from the base class as necessary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by tabstop View Post
    When all else fails, ask ISO.


    Footnote 2 says: "2) Function signatures do not include return type, because that does not participate in overload resolution."
    But what about:

    Code:
    struct base
    {
        virtual operator string ( void ) const;
        
        virtual operator int ( void ) const;
    };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sebastiani
    But what about:
    How are you going to overload a conversion function?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> How are you going to overload a conversion function?

    Well, that's essentially what you're doing. For example:

    Code:
    struct foo
    {
    	operator float ( void ) const
    	{
    		return 0.0f;
    	}
    	
    	operator double ( void ) const
    	{
    		return 1.0;
    	}
    	
    	void bar( float )
    	{	}
    	
    	void bar( double )
    	{	}
    };
    
    int main( void )
    {
    	foo
    		f;
    	int
    		//b1 = f, // error, ambiguous 
    		b2 = ( int )( ( float )f ), // fine
    		b3 = ( int )( ( double )f ); // fine
    	//f.bar( 0 ); // error, ambiguous 	
    	f.bar( ( float )0 ); // fine
    	f.bar( ( double )0 ); // fine
    	return 0;
    }
    So both the implicit operator and bar require overload resolution.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sebastiani
    Well, that's essentially what you're doing.
    Yes, but strictly speaking that is not so. The conversion functions have different names, so by definition there is no overloading (at least not in the context of the same class).

    Quote Originally Posted by Sebastiani
    So both the implicit operator and bar require overload resolution.
    However, for conversion functions is not overload resolution; it is conversion resolution (or whatever name is more appropriate).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Yes, but strictly speaking that is not so. The conversion functions have different names, so by definition there is no overloading.

    Ah, I see what you mean. So they serve a similar purpose, but are actually governed by different rules. Correct?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sebastiani
    Ah, I see what you mean. So they serve a similar purpose, but are actually governed by different rules. Correct?
    Yes, that is how I see it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Sharke View Post
    I think that's actually the case! What I meant was that I can overload a function with another that has a different return type, not that changing the return type is actually necessary for the overload. I guess it was just a clumsy way of rephrasing the fact that the return type has nothing to do with it.
    Are you overloading or overriding the function? You can override a function (in a derived class) with a different return type (although this would be a very bad idea), but you can't overload two functions in the same class with the same name & parameters but different return types.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cpjust
    You can override a function (in a derived class) with a different return type (although this would be a very bad idea)
    That is not true. The virtual function override cannot have a different return type specified, unless that return type is a subtype. In such cases, it can actually be a good idea to make use of such covariant return types.
    Last edited by laserlight; 07-01-2009 at 10:57 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. string::find_last_of function signature
    By WebSnozz in forum C++ Programming
    Replies: 6
    Last Post: 10-09-2002, 09:12 PM