![](/i/fill.gif) |
![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
In C++ it is supported at lead for function templates:
template<class T>
T foo(int x)
{
if(x>10) {
// ... do something interesting
return foo<T>(x-1);
} else {
// ... do something else and finally
return foo<T>(x*3);
}
}
int main()
{
foo<double>(23);
foo<void>(42);
}
Lars R.
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
On 5/11/2011 12:01, Lars R. wrote:
> In C++ it is supported at lead for function templates:
Interesting!
--
Darren New, San Diego CA, USA (PST)
"Coding without comments is like
driving without turn signals."
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Darren New <dne### [at] san rr com> wrote:
> I find it mildly distracting that I can't do this:
> void xyz() { ... }
> void abc()
> {
> if (...)
> return xyz();
> else blah blah blah;
> }
I assumed this to be so in C++ without actually checking. However, now
that I checked it, it seems to work. For example:
//----------------------------------------
#include <iostream>
void bar() { std::cout << "bar()\n"; }
void foo(int i)
{
if(i < 0) return bar();
std::cout << "foo()\n";
}
int main()
{
foo(1);
foo(-1);
}
//----------------------------------------
compiles and runs just fine.
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
On 5/11/2011 12:57, Warp wrote:
> compiles and runs just fine.
Huh. I wonder if that's just a GCC thing or whether that's actually in the
standard. I just *actually* tried it with C# and I get "since XYZ returns
void, the return statement may not be followed by an expression."
--
Darren New, San Diego CA, USA (PST)
"Coding without comments is like
driving without turn signals."
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Darren New <dne### [at] san rr com> wrote:
> On 5/11/2011 12:57, Warp wrote:
> > compiles and runs just fine.
> Huh. I wonder if that's just a GCC thing or whether that's actually in the
> standard. I just *actually* tried it with C# and I get "since XYZ returns
> void, the return statement may not be followed by an expression."
C# isn't C++...
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
>> Huh. I wonder if that's just a GCC thing or whether that's actually in the
>> standard. I just *actually* tried it with C# and I get "since XYZ returns
>> void, the return statement may not be followed by an expression."
>
> C# isn't C++...
Presumably that's the whole point of C++. ;-)
Oh, wait...
--
http://blog.orphi.me.uk/
http://www.zazzle.com/MathematicalOrchid*
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
On 5/11/2011 13:27, Warp wrote:
> C# isn't C++...
I know that. I'm just saying that I was surprised it worked in C++ because
everything I've ever read except the standard implies it shouldn't. Every
tutorial etc etc etc says "You can't put a value after a return in a void
function, and you have to put a value after a return in a non-void
function." So it surprised me this was actually not true for C++. So I
figured I'd try specifically for C#, because maybe all the tutorials for
*that* were wrong. :-)
--
Darren New, San Diego CA, USA (PST)
"Coding without comments is like
driving without turn signals."
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
On 12/05/2011 1:19, Darren New wrote:
> On 5/11/2011 13:27, Warp wrote:
>> C# isn't C++...
>
> I know that. I'm just saying that I was surprised it worked in C++
> because everything I've ever read except the standard implies it
> shouldn't. Every tutorial etc etc etc says "You can't put a value after
> a return in a void function, and you have to put a value after a return
> in a non-void function." So it surprised me this was actually not true
> for C++. So I figured I'd try specifically for C#, because maybe all the
> tutorials for *that* were wrong. :-)
except that your "value" is actually a non-value by itself:
the function "bar" doesn't return anything, so if "foo" returns the
return-value of "bar", it also doesn't return anything.
I suppose it wouldn't work if "bar" really had a return-value.
cu!
--
ZK
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
On 5/13/2011 12:44 AM, Zeger Knaepen wrote:
> On 12/05/2011 1:19, Darren New wrote:
>> On 5/11/2011 13:27, Warp wrote:
>>> C# isn't C++...
>>
>> I know that. I'm just saying that I was surprised it worked in C++
>> because everything I've ever read except the standard implies it
>> shouldn't. Every tutorial etc etc etc says "You can't put a value after
>> a return in a void function, and you have to put a value after a return
>> in a non-void function." So it surprised me this was actually not true
>> for C++. So I figured I'd try specifically for C#, because maybe all the
>> tutorials for *that* were wrong. :-)
>
> except that your "value" is actually a non-value by itself:
>
> the function "bar" doesn't return anything, so if "foo" returns the
> return-value of "bar", it also doesn't return anything.
>
> I suppose it wouldn't work if "bar" really had a return-value.
>
> cu!
Hmm. Interesting point. Its possible that over the long time C++
compilers have been around a test was added to it, which simply tossed
the return functions, in cases where the result would never produce
output. Mind, a warning would have made more sense on those, at least.
Possible you might have an overloaded function, possibly, that would be
void in some cases, or return a value in others, thus it would be
perfectly reasonable to both get nothing from such a function, as well
as return something, in the case of bar(). Bar itself doesn't attempt to
return a value. The other one should, I would think, at least throw a
warning that you are trying to return a result in a function that
doesn't support it. Whether it should be an error, instead of a warning
though...
But, very odd.
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
Le 13/05/2011 10:25, Patrick Elliott a écrit :
> Possible you might have an overloaded function, possibly, that would be
> void in some cases, or return a value in others,
It would be possible in C where prototypes are not mandatory (but
overloading in C is not part of the language).
But I would expect a C++ compiler in C++ mode to barf on a change of
prototype will overloading or a return not matching the declaration.
The return function is not really a function: it put the value inside
the stack and then pop from the current function.
--
A good Manager will take you
through the forest, no mater what.
A Leader will take time to climb on a
Tree and say 'This is the wrong forest'.
Post a reply to this message
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |
| ![](/i/fill.gif) |
|
![](/i/fill.gif) |
|
![](/i/fill.gif) |
| ![](/i/fill.gif) |