Return from non void function?

Discussion in 'C++' started by mlt, Aug 29, 2008.

  1. mlt

    mlt Guest

    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?
     
    mlt, Aug 29, 2008
    #1
    1. Advertising

  2. "mlt" <> 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__
     
    Pascal J. Bourguignon, Aug 29, 2008
    #2
    1. Advertising

  3. 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
     
    Obnoxious User, Aug 29, 2008
    #3
  4. mlt

    raj s Guest

    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
     
    raj s, Aug 29, 2008
    #4
  5. mlt

    James Kanze Guest

    On Aug 29, 4:08 pm, "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?


    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:
    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
     
    James Kanze, Aug 31, 2008
    #5
  6. mlt

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Aug 31, 2008
    #6
  7. 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
     
    Thomas J. Gritzan, Aug 31, 2008
    #7
  8. mlt

    James Kanze Guest

    On Aug 31, 8:04 pm, "Thomas J. Gritzan" <>
    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:
    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
     
    James Kanze, Sep 1, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    585
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    829
    The Real OS/2 Guy
    Oct 28, 2003
  3. David M. Wilson
    Replies:
    8
    Views:
    501
    Ben Pfaff
    Jan 7, 2004
  4. Replies:
    5
    Views:
    865
    S.Tobias
    Jul 22, 2005
  5. Replies:
    1
    Views:
    429
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page