How to return from template class if variable is not initialized

A

Ami

Hi All,
I am facing a very basic problem of returning a value from template based stack class where It is possible that stack has no values to pop but as per declaration it must return <type> value.

template <class T> T stack<T>::pop()
{
T val;
if (empty())
{
return val;
}
return nodes[top--];
}

Any help is highly appreciated.
Thanks.
 
B

Balog Pal

Ami said:
Hi All,
I am facing a very basic problem of returning a value from
template based stack class where It is possible that stack has
no values to pop but as per declaration it must return <type> value.

We have a plenty of articles around that explain why a 'pop' function that
returns a value is a bad idea. And why instead separeate T& top() and void
pop() are suggested.

As for the empty stack problem, normally it is a precondition for pop/top
that stack is not empty. So on that condition if you check at all, may
terminate() or throw an exception. If you resort to return something it is
certanly up to you to design and document what it is. like return T(), or
return a pre-created instance that may be a const static for the class or
passed to the constructor at runtime.
 
P

Paul

Paavo Helde said:
com:
Hi All,
I am facing a very basic problem of returning a value from template
based stack class where It is possible that stack has no values to
pop but as per declaration it must return <type> value.

template <class T> T stack<T>::pop()
{
T val;
if (empty())
{
return val;
}
return nodes[top--];
}

It depends on what you want to do. If calling pop() on an empty container
is an error, then you should throw an exception instead. If it is not an
error, then you have to return some kind of dummy value. If type T is
default-constructible, then you can return a default-constructed value by

return T();

In general, this is not a very good idea as the caller cannot easily tell
if the result is a real or a dummy value. There are ways to return more
than a single value (e.g. with a Fallible type or with extra reference
parameters), but this makes the interface clumsy and caller code more
complicated. One possible solution is to provide two overloads: the
simple pop() would throw if the stack is empty, and the other pop() would
take a default value argument which is returned if the stack is empty:

template <class T> T stack<T>::pop(const T& defaultvalue)
{
if (empty())
{
return defaultvalue;
}
// ...

This way, if the caller does not really care if the stack is empty and is
happy to use a dummy value in this case, it can easily and explicitly
indicate this by providing a suitable dummy value by itself.

To invoke this function would you not need to call it with a parameter, i.e:
T t;
pstack->pop(t);
 
W

Werner

com:
Hi All,
   I am facing a very basic problem of returning a value from template
   based stack class where It is possible that stack has no valuesto
   pop but as per declaration it must return <type> value.
template <class T> T stack<T>::pop()
{
     T val;
     if (empty())
     {
          return val;
     }
     return nodes[top--];
}
It depends on what you want to do. If calling pop() on an empty container
is an error, then you should throw an exception instead. If it is not an
error, then you have to return some kind of dummy value. If type T is
default-constructible, then you can return a default-constructed value by
return T();
In general, this is not a very good idea as the caller cannot easily tell
if the result is a real or a dummy value. There are ways to return more
than a single value (e.g. with a Fallible type or with extra reference
parameters), but this makes the interface clumsy and caller code more
complicated. One possible solution is to provide two overloads: the
simple pop() would throw if the stack is empty, and the other pop() would
take a default value argument which is returned if the stack is empty:
template <class T> T stack<T>::pop(const T& defaultvalue)
{
  if (empty())
  {
      return defaultvalue;
  }
  // ...
To invoke this function would you not need to call it with a parameter, i..e:

This depends on the function declaration, which
would be specified in the class definition.

perhaps:

//...Class def
T pop( const T& = T() );

};
 
P

Paul

com:
Hi All,
I am facing a very basic problem of returning a value from template
based stack class where It is possible that stack has no values to
pop but as per declaration it must return <type> value.
template <class T> T stack<T>::pop()
{
T val;
if (empty())
{
return val;
}
return nodes[top--];
}
It depends on what you want to do. If calling pop() on an empty
container
is an error, then you should throw an exception instead. If it is not an
error, then you have to return some kind of dummy value. If type T is
default-constructible, then you can return a default-constructed value
by
return T();
In general, this is not a very good idea as the caller cannot easily
tell
if the result is a real or a dummy value. There are ways to return more
than a single value (e.g. with a Fallible type or with extra reference
parameters), but this makes the interface clumsy and caller code more
complicated. One possible solution is to provide two overloads: the
simple pop() would throw if the stack is empty, and the other pop()
would
take a default value argument which is returned if the stack is empty:
template <class T> T stack<T>::pop(const T& defaultvalue)
{
if (empty())
{
return defaultvalue;
}
// ...
To invoke this function would you not need to call it with a parameter,
i.e:

--This depends on the function declaration, which
--would be specified in the class definition.

--perhaps:

--//...Class def
--T pop( const T& = T() );
The above parameter needs an identifier, but even if this was corrected the
call would be ambiguous.

int foo(){ return 0;}
int foo(int param=0){ return 1;}

int main(){
int x = foo();
}


error C2668: 'foo': ambiguous call to overloaded function.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top