POV-Ray : Newsgroups : povray.off-topic : Computer language quirks Server Time
30 Jul 2024 02:17:23 EDT (-0400)
  Computer language quirks (Message 14 to 23 of 33)  
<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>
From: Lars R 
Subject: Re: Computer language quirks n C++ :-)
Date: 11 May 2011 15:01:13
Message: <4dcadcf9$1@news.povray.org>
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

From: Darren New
Subject: Re: Computer language quirks n C++ :-)
Date: 11 May 2011 15:13:24
Message: <4dcadfd4$1@news.povray.org>
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

From: Warp
Subject: Re: Computer language quirks
Date: 11 May 2011 15:57:32
Message: <4dcaea2c@news.povray.org>
Darren New <dne### [at] sanrrcom> 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

From: Darren New
Subject: Re: Computer language quirks
Date: 11 May 2011 16:17:27
Message: <4dcaeed7$1@news.povray.org>
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

From: Warp
Subject: Re: Computer language quirks
Date: 11 May 2011 16:27:00
Message: <4dcaf114@news.povray.org>
Darren New <dne### [at] sanrrcom> 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

From: Orchid XP v8
Subject: Re: Computer language quirks
Date: 11 May 2011 16:52:00
Message: <4dcaf6f0$1@news.povray.org>
>> 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

From: Darren New
Subject: Re: Computer language quirks
Date: 11 May 2011 19:19:36
Message: <4dcb1988@news.povray.org>
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

From: Zeger Knaepen
Subject: Re: Computer language quirks
Date: 13 May 2011 03:44:28
Message: <4dcce15c$1@news.povray.org>
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

From: Patrick Elliott
Subject: Re: Computer language quirks
Date: 13 May 2011 04:25:08
Message: <4dcceae4$1@news.povray.org>
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

From: Le Forgeron
Subject: Re: Computer language quirks
Date: 13 May 2011 04:46:56
Message: <4dccf000$1@news.povray.org>
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

<<< Previous 10 Messages Goto Latest 10 Messages Next 10 Messages >>>

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.