Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Return from non void function?

Reply
Thread Tools

Return from non void function?

 
 
mlt
Guest
Posts: n/a
 
      08-29-2008
I have a function that returns a type T:

T f(){

T t;

....

if(something){
return;
}
....


return t;


}

But first it checks for some conditions and returns if they are not
fullfilled. The problem is that I have declared the function to return type
T so I can't just make it return "nothing".

Is there at way to return from a function without returning a value?


 
Reply With Quote
 
 
 
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      08-29-2008
"mlt" <(E-Mail Removed)> writes:

> I have a function that returns a type T:
> But first it checks for some conditions and returns if they are not
> fullfilled. The problem is that I have declared the function to return type
> T so I can't just make it return "nothing".
>
> Is there at way to return from a function without returning a value?



Yes, throwing an exception:


T f(){
T t;
if(something){
throw "Something occured that prevents me to return a t.";
}
return t;
}


void g(){
try{
T t=f();
}catch(const char* e){
std::cout<<e<<std::endl;
}
}


In general however, it's better to throw an object of some class
(subclass of std::exception), so you can pass over part of the state
to qualify the exception and perhaps let the catcher try to recover
from the problem more elegantly.

--
__Pascal Bourguignon__
 
Reply With Quote
 
 
 
 
Obnoxious User
Guest
Posts: n/a
 
      08-29-2008
On Fri, 29 Aug 2008 16:08:10 +0200, mlt wrote:

> I have a function that returns a type T:
>
> T f(){
>
> T t;
>
> ...
>
> if(something){
> return;
> }
> ...
>
>
> return t;
>
>
> }
>
> But first it checks for some conditions and returns if they are not
> fullfilled. The problem is that I have declared the function to return
> type T so I can't just make it return "nothing".
>
> Is there at way to return from a function without returning a value?


In these cases I usually declare the function as
bool f(T &);

--
OU
 
Reply With Quote
 
raj s
Guest
Posts: n/a
 
      08-29-2008
On Aug 29, 7:38*pm, Obnoxious User <O...@127.0.0.1> wrote:
> On Fri, 29 Aug 2008 16:08:10 +0200, mlt wrote:
> > I have a function that returns a type T:

>
> > T f(){

>
> > T t;

>
> > ...

>
> > if(something){
> > * * return;
> > }
> > ...

>
> > return t;

>
> > }

>
> > But first it checks for some conditions and returns if they are not
> > fullfilled. The problem is that I have declared the function to return
> > type T so I can't just make it return "nothing".

>
> > Is there at way to return from a function *without returning a value?

>
> In these cases I usually declare the function as
> bool f(T &);
>
> --
> OU


with bool as return type u have to check every time is the return is
correct or not ..but with exception u dont..u can throw error catch it
and decide to proceed based on ur wish
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-31-2008
On Aug 29, 4:08 pm, "mlt" <(E-Mail Removed)> wrote:
> I have a function that returns a type T:


> T f(){


> T t;


> ...


> if(something){
> return;}


> ...


> return t;
> }


> But first it checks for some conditions and returns if they
> are not fullfilled. The problem is that I have declared the
> function to return type T so I can't just make it return
> "nothing".


> Is there at way to return from a function without returning a
> value?


As others have explained, no. The "canonical" solution in such
cases is to return a Fallible; a template class which associates
a bool (or a state type) and some other type.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-31-2008
Alf P. Steinbach wrote:

> * James Kanze:
>>
>> Fallible

>
> I seem to remember you had another name for that concept, but I can't
> remember what?


I think, James consistently uses the term Fallible. When I ran into the need
for some such thing, I called it box<T> (the idea being that a box can be
be empty or contain exactly one item of type T).


Best

Kai-Uwe Bux
 
Reply With Quote
 
Thomas J. Gritzan
Guest
Posts: n/a
 
      08-31-2008
Kai-Uwe Bux schrieb:
> Alf P. Steinbach wrote:
>
>> * James Kanze:
>>> Fallible

>> I seem to remember you had another name for that concept, but I can't
>> remember what?


AFAIK, James called it Fallible all the time, coming from Barton and
Nackman's Fallible<>. It seems that James extended it to hold an
additional error code.

> I think, James consistently uses the term Fallible. When I ran into the need
> for some such thing, I called it box<T> (the idea being that a box can be
> be empty or contain exactly one item of type T).


Boost calls it optional<T>.

--
Thomas
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      09-01-2008
On Aug 31, 8:04 pm, "Thomas J. Gritzan" <(E-Mail Removed)>
wrote:
> Kai-Uwe Bux schrieb:


> > Alf P. Steinbach wrote:


> >> * James Kanze:
> >>> Fallible
> >> I seem to remember you had another name for that concept, but I can't
> >> remember what?


> AFAIK, James called it Fallible all the time, coming from
> Barton and Nackman's Fallible<>. It seems that James extended
> it to hold an additional error code.


Yup. And very, very recently, to not require a default
constructor.

> > I think, James consistently uses the term Fallible. When I
> > ran into the need for some such thing, I called it box<T>
> > (the idea being that a box can be be empty or contain
> > exactly one item of type T).


> Boost calls it optional<T>.


Which expresses a different intention. The basic principle is
the same, however, and I think that the Boost version is often
used for "fallible", and I've used my own Fallible a couple of
times where the intention was "optional", or more generally
conditionally present, but with no implication of "failure".

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
CAN WE TYPE CAST AN INTEGER AS (VOID *)X..(Like can I return a value (void *)x) Abhishek C Programming 12 01-30-2006 03:22 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments