Click here to Skip to main content
Licence CPOL
First Posted 30 Mar 2007
Views 58,737
Downloads 281
Bookmarked 28 times

Function Overload by Return

By | 5 Apr 2007 | Article
Choosing the right function based on the return type

Introduction

This code shows one way to use the C++ overload rule using the function return type instead of its signature. Of course, nothing is changed in the language, and the result just looks like a return type overload.

Background

The function overload, in other words, reusing an existent function name but with different parameters, is something more or less known by the C++, Java and .NET community. As a sine qua non condition, we know that the overloaded function signature must be always different from its homonyms colleagues. The code below illustrates this:

GUID guid;
wstring guidS;

CreateNewGUID(guidS); // calls void CreateNewGUID(wstring&)
CreateNewGUID(guid);  // calls void CreateNewGUID(GUID&) (the compiler knows that)

It's a pretty common way to accept different input formats. But what about the output? How to extract different information or the same information in different format using the overload feature (e.g. the above example) without passing the result variable as a output argument? Giving ourselves some imagination and thinking how a valid construction would be, we can think of something like this:

GUID guid;
wstring guidS;

guidS = CreateNewGUID(); // calls wstring CreateNewGUID()
guid = CreateNewGUID();  // calls GUID CreateNewGUID() (the compiler knows that?)

Opening again our old and hurt C++ theories book, we can see the above code DOES NOT work. Or, at least, should not be. Just defining a couple of functions like that causes the following error:

error C2556: 'GUID CreateNewGUID(void)' : overloaded function differs 
	only by return type from 'std::wstring CreateNewGUID(void)'

That's right. Well, the error is right. The return type is not a function property that identifies it uniquely. Only the signature can do that (the parameters received by the function). AFAIK, this "limitation" is hack proof. Anyway, nothing disallows us to use another feature besides ordinary functions:

struct CreateNewGUID
{
   // what is supposed to be here?
};

We got it! Now we can "call" our "function" creating a new instance of the struct and attributing the "return" to a wstring or to our GUID struct:

guidS = CreateNewGUID(); 	// instantiates a CreateNewGUID
guid = CreateNewGUID(); 	// instantiates a CreateNewGUID. 
			// The difference is in the "return"

Using the Code

Since we create a new type, and knowing that this new type is different from the already known wstring and GUID types, we should simply convert our new type into every desirable return type:

struct CreateNewGUID
{
   operator wstring () { ... } 	// the conversion is the "function call"

   operator GUID () { ... } 	// as there's a couple of conversions... 
				// over... underload!
};

This concludes our somewhat odd solution to overload a "function" by the return type:

// instantiates a CreateNewGUID e calls CreateNewGUID::operator wstring()
guidS = CreateNewGUID();

// instantiates a CreateNewGUID e calls CreateNewGUID::operator GUID()
guid = CreateNewGUID();

There's the entire source.

/** @file underload.cpp
@brief A way to make overload using just the return type.
@author Wanderley Caloni
*/
#include <windows.h>
#include <objbase.h>

#include <iostream />
#include <string>

using namespace std;

struct CreateNewGUID
{
   operator wstring () // the first function...
   {
      GUID guid = operator GUID();
      OLECHAR buf[40] = { };
      ::StringFromGUID2(guid, buf, sizeof(buf));
      return wstring(buf);
   }

   operator GUID () // ... and its "underload"
   {
      GUID guid = { };
      ::CoCreateGuid(&guid);
      return guid;
   }
};

Points of Interest

This code was developed a long time ago as an answer to a friend's doubt. I learnt while coding that C++ is even more powerful than we think it is. In one form or another, we can always get what we want.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Wanderley Caloni

Web Developer

Brazil Brazil

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
SuggestionAlternative [modified] PinmemberPascal Damman2:49 16 Sep '11  
GeneralNice Article PinmemberGuimaSun3:33 8 May '07  
GeneralRe: Nice Article PinmemberWanderley Caloni4:25 8 May '07  
QuestionHow to Create member fcuntions like what u said? Pinmembershtarwaterloo17:46 16 Apr '07  
AnswerRe: How to Create member fcuntions like what u said? PinmemberWanderley Caloni3:34 17 Apr '07  
GeneralRe: How to Create member fcuntions like what u said? Pinmembershtarwaterloo15:43 24 Apr '07  
GeneralRe: How to Create member fcuntions like what u said? Pinmembersylvaticus1:35 7 Aug '07  
GeneralRe: How to Create member fcuntions like what u said? PinmemberWanderley Caloni3:43 7 Aug '07  
GeneralThanks Pinmemberneurorebel_2:33 6 Apr '07  
GeneralRe: Thanks PinmemberWanderley Caloni5:38 6 Apr '07  
GeneralRe: Thanks Pinmemberlallous21:45 9 Apr '07  
GeneralRe: Thanks PinmemberWanderley Caloni2:31 10 Apr '07  
QuestionDoes Second approach work? PinmemberChenna A Reddy19:30 5 Apr '07  
AnswerRe: Does Second approach work? PinmemberWanderley Caloni22:52 5 Apr '07  
GeneralTry Compiling it with GCC Pinmemberetkins5:15 5 Apr '07  
GeneralRe: Try Compiling it with GCC PinmemberWanderley Caloni7:19 5 Apr '07  
QuestionTemplate functions ? Pinmemberchatulevich1:51 4 Apr '07  
AnswerRe: Template functions ? PinmemberWanderley Caloni2:46 4 Apr '07  
GeneralRe: Template functions ? Pinmemberchatulevich3:03 4 Apr '07  
GeneralGreat! PinmemberMichael2721:50 2 Apr '07  
GeneralRe: Great! PinmemberWanderley Caloni3:24 3 Apr '07  
GeneralMisuse of Conversion Operators [modified] PinmemberGeorge L. Jackson13:07 2 Apr '07  
GeneralRe: Misuse of Conversion Operators PinmemberETA21:55 2 Apr '07  
GeneralRe: Misuse of Conversion Operators PinmemberGeorge L. Jackson23:54 2 Apr '07  
GeneralRe: Misuse of Conversion Operators Pinmember*bilk*2:09 3 Apr '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 6 Apr 2007
Article Copyright 2007 by Wanderley Caloni
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid