Topic: Template return type question


Author: Erik Badger <etbadger@hotmail.com>
Date: 1998/03/15
Raw View
I believe that the standard allows a void function to explicityly return void,
but many compilers (e.g. MSVC) don't support this yet.

-Erik

Xavier Tarrago wrote:

> Hello everybody,
>
>   My question is about template syntax. From Stroustrup 3rd ed.,
> I tried to setup mem_fun object (p. 521).
> ...
>   All goes right for the 1st case, but when return type is void, I get
> an internal error. IMHO, it is due to the statement
> return f() in a void function where f is void. According to (d)std,
> should
> the compiler handle this case ?
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Xavier Tarrago <Xavier.Tarrago@cea.fr>
Date: 1998/03/12
Raw View
Hello everybody,

  My question is about template syntax. From Stroustrup 3rd ed.,
I tried to setup mem_fun object (p. 521).

#include <functional>
#include <iostream.h>
#include <vector>

template< class R, class T>
class mem_fun_t: public unary_function<T*, R> {
  R (T::*pmf)();
public:
  explicit mem_fun_t(R (T::*p)()) : pmf( p) {}
  R operator()( T* p) const {return (p->*pmf)();}
};

template< class R, class T>
mem_fun_t<R,T> mem_fun( R (T::*f)())
{ return mem_fun_t<R,T>( f);}  // Offending if R is void

class Shape
{
public:
  void void_draw() { cerr << "void draw" << endl;}
  int int_draw() { cerr << "int draw" << endl; return 0;}
};

int main()
{
  vector<Shape*> vsh( 5);
  for( unsigned int i=0; i < vsh.size(); i++)
  {
    vsh[i] = new Shape;
  }
  for_each( vsh.begin(), vsh.end(), mem_fun( &Shape::int_draw));  // 1
(ok)
  for_each( vsh.begin(), vsh.end(), mem_fun( &Shape::void_draw)); // 2
(pb)
}

  All goes right for the 1st case, but when return type is void, I get
an internal error. IMHO, it is due to the statement
return f() in a void function where f is void. According to (d)std,
should
the compiler handle this case ?
--
+--------------------------------------------------------------------------+
| X.Tarrago - mailto:Xavier.Tarrago@cea.fr
|  Commissariat a l'Energie Atomique |
|  Centre de Saclay - bat 611        | tel : 01 69 08 96 49
|  91191 Gif sur Yvette CEDEX        | fax : 01 69 08 75 97
|  France                            |
+------------------------------------+-------------------------------------+
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]