Return from non void function?

M

mlt

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?
 
P

Pascal J. Bourguignon

mlt said:
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.
 
O

Obnoxious User

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 &);
 
R

raj s

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

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
 
J

James Kanze

I have a function that returns a type T:
T t;



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.
 
K

Kai-Uwe Bux

Alf said:
* James Kanze:

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
 
T

Thomas J. Gritzan

AFAIK, James called it Fallible all the time, coming from Barton and
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>.
 
J

James Kanze

Kai-Uwe Bux schrieb:
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.
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".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top