Topic: Return


Author: "Chris Minnoy" <cminnoy@starlab.net>
Date: 1998/12/31
Raw View
Consider this:

Suppose you wanna, for some reason, make a class that can have a pointer
to a function and when you call the operator() of that class you call that
function.
This could look like this:

int f1( int a, int b )
{
    return a + b;
}

void f2( int a, int b )
{
    std::cout << ( a * b );
}

template <class _A1, class _A2, class _R>
class Something
{
public:
    _R (*f)( _A1&, _A2& );

    _R operator()( _A1& a, _A2& b ) { return f( a, b ); } // problem point
};

int main()
{
    // so far no problem
    Something<int,int,int> a;
    a.f = f1;
    a( 5, 3 );
    // now the throuble starts
    Something<int,int,void> b;
    b.f = f2;
    b( 5, 3 );
}

The meaning of f1 and f2 is not relevant.
As long as you can give a type other than void as return type, all
works well. However, like in f2, you don't want to return something.
In operator() i wrote : return f( a, b );
The compiler doesn't except  this because it thinks i wanna return
something and that's not allowed because operator() has a void
return type.
Wouldn't it be nice if the compiler just knew that f2 doesn't return
anything and makes no fuss about it.
As far as I know, it doesn't violate anything if the compiler let
this notation be the equivalent of:

f2( a, b );            =>    return f2( a, b );
return;

as long as the called expression (f2) returns nothing.

Ok, I know you can make a specialised template class out of
the general template class, but that's a lot of writing just for
something silly.
What do you think?

Chris Minnoy
cminnoy@starlab.net
cminnoy@vub.ac.be
http://www.starlab.net



[ 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: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1998/12/31
Raw View
On 31 Dec 1998 16:38:43 GMT, Chris Minnoy <cminnoy@starlab.net> wrote:

>int f1( int a, int b )
>void f2( int a, int b )

>template <class _A1, class _A2, class _R>
>class Something
>{
>public:
>    _R (*f)( _A1&, _A2& );
>
>    _R operator()( _A1& a, _A2& b ) { return f( a, b ); } // problem point
>};

Why not pass by value or by const reference?
Can initialize 'f' in the ctor.
And 'f' can be const (const goes to the right of the star).

Names beginning with an underscore and followed by an
uoppercase letter, names beginning with two underscores,
and sometimes names beginning with one underscore and followed by
any letter are reserved for the implementation.

In any case, 'void' returns are allowed by the standard.
The reason is to make template instantiations work properly, as
in your case.  It's a new feature, and neither of my two
compilers supports it.  /Don't have the standard with me,
but I think the quote is somewhere from chapter 7.
As a workaround, makj, make a specialization for _R==void.

>    Something<int,int,int> a;
>    a.f = f1;

This should not compile because int(*)(int) us not the
same as int(*)(int&).

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------


[ 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: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/12/31
Raw View
"Chris Minnoy" <cminnoy@starlab.net> writes:

>Wouldn't it be nice if the compiler just knew that f2 doesn't return
>anything and makes no fuss about it.
>As far as I know, it doesn't violate anything if the compiler let
>this notation be the equivalent of:

>f2( a, b );            =>    return f2( a, b );
>return;

>as long as the called expression (f2) returns nothing.

The original rule in C++ was the same as the rule in C:
you could not use a void expression in a return statement.

The standard in 6.6.3 "The return statement" says you can do
you can use a void expression in a return statement if
the function has a void return type. Its purpose is primarily
to support templates.

I don't know how many compilers support this new rule yet.

--
Steve Clamage, stephen.clamage@sun.com


[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/12/31
Raw View
In article <915098032.726923@saturn.riv.be>, Chris Minnoy
<cminnoy@starlab.net> writes
>As far as I know, it doesn't violate anything if the compiler let
>this notation be the equivalent of:
>
>f2( a, b );            =>    return f2( a, b );
>return;
>
>as long as the called expression (f2) returns nothing.

What compiler are you using?  We fixed this in the Standard about two
years ago.

e.g.

void fn(int);
void gn(int i){ return fn(i); }

is correct C++



Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ 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: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1998/12/31
Raw View
In article <76gbd2$rnr$1@engnews2.Eng.Sun.COM>, Steve Clamage
<stephen.clamage@sun.com> writes
>The original rule in C++ was the same as the rule in C:
>you could not use a void expression in a return statement.
>
>The standard in 6.6.3 "The return statement" says you can do
>you can use a void expression in a return statement if
>the function has a void return type. Its purpose is primarily
>to support templates.
>
>I don't know how many compilers support this new rule yet.

CodeWarrior Pro 4 does.


Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ 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: AllanW@my-dejanews.com
Date: 1998/12/31
Raw View
In article <915098032.726923@saturn.riv.be>,
  "Chris Minnoy" <cminnoy@starlab.net> wrote:
[about a template class with a function's return type as a parameter]
> Wouldn't it be nice if the compiler just knew that f2 doesn't return
> anything and makes no fuss about it.
> As far as I know, it doesn't violate anything if the compiler let
> this notation be the equivalent of:
>
> f2( a, b );            =>    return f2( a, b );
> return;
>
> as long as the called expression (f2) returns nothing.
>
> Ok, I know you can make a specialised template class out of
> the general template class, but that's a lot of writing just for
> something silly.

According to the C++ standard, in 6.6.3:

  3 A return statement with expression of type "cv void" can be
    used only in functions with a return type of cv void; the
    expression is evaluated just before the function returns to
    its caller.

In other words, what you want to do is legal.

However, that was a fairly late change to the standard, so many
compilers do not yet support it. For instance, VC++ 5.0 does not
allow this.

Fortunately, you have an easy workaround: That which you dubbed
"a lot of writing just for something silly." Specialize your
template for return types of void.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own


[ 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              ]