func return question

R

RB

I am relatively inexperienced in C++ but trying to learn. I ran across this
in my studies. My question is not about the declspec since I understand that
it keeps the prolog and epilog out of the declared function.
My question rather is exactly what is going on with throw( ) . I.e. is throw
returning the previous function ?
------------------------------------------------
inline __declspec(naked) unsigned __fastcall Xadd(volatile unsigned* t, int x) throw()
{
__asm mov eax, edx
__asm lock xadd dword ptr [ecx], eax
__asm ret
}
 
C

cpp4ever

I am relatively inexperienced in C++ but trying to learn. I ran across this
in my studies. My question is not about the declspec since I understand
that
it keeps the prolog and epilog out of the declared function.
My question rather is exactly what is going on with throw( ) . I.e. is
throw
returning the previous function ?
------------------------------------------------
inline __declspec(naked) unsigned __fastcall Xadd(volatile unsigned* t,
int x) throw()
{
__asm mov eax, edx
__asm lock xadd dword ptr [ecx], eax
__asm ret
}

That is in-line assembly language and as it stands is compiler
dependent, so I would ignore the code. The throw() informs the compiler
that the the function can only throw a general exception. If my memory
serves me correctly regarding exception syntax.

cop4ever
 
A

AnonMail2005

I am relatively inexperienced in C++ but trying to learn. I ran across this
in my studies. My question is not about the declspec since I understand that
it keeps the prolog and epilog out of the declared function.
  My question rather is exactly what is going on with throw( ) . I.e. is throw
returning the previous function ?
------------------------------------------------
inline __declspec(naked)  unsigned __fastcall Xadd(volatile unsigned* t, int x)  throw()
{
   __asm      mov  eax, edx
   __asm lock xadd dword ptr [ecx], eax
   __asm      ret

}

That's an exception spec. This spec says that the function will not
throw any exceptions. If for some reason the function does throw an
exception (or throws an exception that is not specified in the spec),
a predefined function void unexpected() is called. The default action
is to call terminate().

HTH
 
A

Alf P. Steinbach /Usenet

* (e-mail address removed), on 30.06.2010 15:41:
I am relatively inexperienced in C++ but trying to learn. I ran across this
in my studies. My question is not about the declspec since I understand that
it keeps the prolog and epilog out of the declared function.
My question rather is exactly what is going on with throw( ) . I.e. is throw
returning the previous function ?
------------------------------------------------
inline __declspec(naked) unsigned __fastcall Xadd(volatile unsigned* t, int x) throw()
{
__asm mov eax, edx
__asm lock xadd dword ptr [ecx], eax
__asm ret

}

That's an exception spec. This spec says that the function will not
throw any exceptions. If for some reason the function does throw an
exception (or throws an exception that is not specified in the spec),
a predefined function void unexpected() is called. The default action
is to call terminate().

Since the OP is using MSVC: MSVC just ignores exception specifications.

And in C++0x they'll be removed or at least deprecated.


Cheers,

- Alf
 
C

cpp4ever

* (e-mail address removed), on 30.06.2010 15:41:
I am relatively inexperienced in C++ but trying to learn. I ran
across this
in my studies. My question is not about the declspec since I
understand that
it keeps the prolog and epilog out of the declared function.
My question rather is exactly what is going on with throw( ) .
I.e. is throw
returning the previous function ?
------------------------------------------------
inline __declspec(naked) unsigned __fastcall Xadd(volatile unsigned*
t, int x) throw()
{
__asm mov eax, edx
__asm lock xadd dword ptr [ecx], eax
__asm ret

}

That's an exception spec. This spec says that the function will not
throw any exceptions. If for some reason the function does throw an
exception (or throws an exception that is not specified in the spec),
a predefined function void unexpected() is called. The default action
is to call terminate().

Since the OP is using MSVC: MSVC just ignores exception specifications.

And in C++0x they'll be removed or at least deprecated.


Cheers,

- Alf

Thanks for the info Alf, assuming C++0x, (or should that now be C++1x?),
has that finalised. For the time being I'll use C++ according to the
latest published standard, around 1998 I believe.

regards

cpp4ever
 
R

RB

------------------------------------------------
inline __declspec(naked) unsigned __fastcall Xadd(volatile unsigned* t, int x) throw()
{
__asm mov eax, edx
__asm lock xadd dword ptr [ecx], eax
__asm ret

}
That's an exception spec. This spec says that the function will not
throw any exceptions. If for some reason the function does throw an
exception (or throws an exception that is not specified in the spec),
a predefined function void unexpected( ) is called. The default action
is to call terminate( ).
---------
Oh ok, so this is a throw specific thing, so there could not be just any
function in it's place?
Also below is a similar one that the asm code appears to be saving the previous
exception handler on the list (I presume at FS:[0 or whatever] ) and then calling
a generic custom exception handler, if I understand you correctly the "throw" in
this case is doing the same thing ?
/*
typedef enum _EXCEPTION_DISPOSITION {
ExceptionContinueExecution,
ExceptionContinueSearch,
ExceptionNestedException,
ExceptionCollidedUnwind
} EXCEPTION_DISPOSITION;
// other enums and defines in header
*/

namespace my_handler
{
__declspec (naked)
EXCEPTION_DISPOSITION my_exc_handler(
_EXCEPTION_RECORD *ExceptionRecord,
void * EstablisherFrame,
struct _CONTEXT *ContextRecord,
void * DispatcherContext) throw( )
{
_asm
{
push EBP //prolog
mov EBP, ESP

push DispatcherContext
push ContextRecord
push EstablisherFrame
push ExceptionRecord
push EAX
call _my_exc_handler

mov ESP, EBP //epilog
pop EBP
ret
}
}
 
A

Alf P. Steinbach /Usenet

* RB, on 30.06.2010 16:27:
------------------------------------------------
inline __declspec(naked) unsigned __fastcall Xadd(volatile unsigned*
t, int x) throw()
{
__asm mov eax, edx
__asm lock xadd dword ptr [ecx], eax
__asm ret

}
That's an exception spec. This spec says that the function will not
throw any exceptions. If for some reason the function does throw an
exception (or throws an exception that is not specified in the spec),
a predefined function void unexpected( ) is called. The default action
is to call terminate( ).

Not sure what you mean.


Also below is a similar one that the asm code appears to be saving the
previous
exception handler on the list (I presume at FS:[0 or whatever] ) and
then calling a generic custom exception handler, if I understand you
correctly the "throw" in this case is doing the same thing ?

For a standard-conforming compiler it would, yes, except that 'unexpected' never
returns. It rethrows or terminates.

Also note that the asm code you present (1) isn't C++ :), and (2) isn't
necessarily handling a C++ exception, it's handling a Windows SEH exception
(which might or might not represent a C++ exception).

/*
typedef enum _EXCEPTION_DISPOSITION {
ExceptionContinueExecution,
ExceptionContinueSearch,
ExceptionNestedException,
ExceptionCollidedUnwind
} EXCEPTION_DISPOSITION;
// other enums and defines in header
*/

namespace my_handler
{
__declspec (naked) EXCEPTION_DISPOSITION my_exc_handler(
_EXCEPTION_RECORD *ExceptionRecord,
void * EstablisherFrame,
struct _CONTEXT *ContextRecord,
void * DispatcherContext) throw( )
{
_asm
{
push EBP //prolog
mov EBP, ESP

push DispatcherContext
push ContextRecord
push EstablisherFrame
push ExceptionRecord
push EAX
call _my_exc_handler

mov ESP, EBP //epilog
pop EBP
ret
}
}


Cheers & hth.,

- Alf
 
R

RB

Not sure what you mean.

Ugh well excuse my C++ ignorance, but is it possible to put a function
that returns a function of type
unsigned __fastcall Xadd(volatile unsigned* t, int x) FuncWhatever( );
if that is even possible? In other words I understand that I could return
a ptr to a function, but could it (or what good would it do to) return a function?
I am probably going out in left field on this since you have already explained
the throw here. But I am just trying to learn the semantics of C++.
 
C

cpp4ever

* (e-mail address removed), on 30.06.2010 15:41:
I am relatively inexperienced in C++ but trying to learn. I ran
across this
in my studies. My question is not about the declspec since I
understand that
it keeps the prolog and epilog out of the declared function.
My question rather is exactly what is going on with throw( ) .
I.e. is throw
returning the previous function ?
------------------------------------------------
inline __declspec(naked) unsigned __fastcall Xadd(volatile unsigned*
t, int x) throw()
{
__asm mov eax, edx
__asm lock xadd dword ptr [ecx], eax
__asm ret

}

That's an exception spec. This spec says that the function will not
throw any exceptions. If for some reason the function does throw an
exception (or throws an exception that is not specified in the spec),
a predefined function void unexpected() is called. The default action
is to call terminate().

Since the OP is using MSVC: MSVC just ignores exception specifications.

And in C++0x they'll be removed or at least deprecated.


Cheers,

- Alf

Thanks for the info Alf, assuming C++0x, (or should that now be C++1x?),

No, it's C++0x.
has that finalised. For the time being I'll use C++ according to the
latest published standard, around 1998 I believe.

2003.

Exception specifications are deprecated in C++0x. That doesn't mean that
their meaning has changed, nor that you shouldn't use them. It's a
warning that in some future standard they might be removed.

Thanks again, I have that published 2003 standard book. That's not a
feature of C++ I use frequently, and given the C++0x standard
information kindly provided I won't be doing so in future.

cpp4ever
 
B

Bo Persson

RB said:
Ugh well excuse my C++ ignorance, but is it possible to put a
function that returns a function of type
unsigned __fastcall Xadd(volatile unsigned* t, int x)
FuncWhatever( ); if that is even possible? In other words I
understand that I could
return a ptr to a function, but could it (or what good would it do
to)
return a function? I am probably going out in left field on this
since you have already explained the throw here. But I am just
trying to learn the semantics of C++.

No, you can't return a function, but possibly a pointer or a reference
to a function. The way of doing that is so ugly that I will not even
attempt to show what it look like. :)

The throw at the end of the function declaration is a keyword that
cannot be replaced by some function. It says "throw" and then there is
a list of the possible exceptions thrown by the function. In your case
the list is empty, (), meaning that the function throws nothing.



Bo Persson
 
C

cpp4ever

Ugh well excuse my C++ ignorance, but is it possible to put a function
that returns a function of type
unsigned __fastcall Xadd(volatile unsigned* t, int x) FuncWhatever( );
if that is even possible? In other words I understand that I could return
a ptr to a function, but could it (or what good would it do to) return a
function?
I am probably going out in left field on this since you have already
explained the throw here. But I am just trying to learn the semantics of
C++.

Folks are happy to help you with C++ semantics here. I suspect that Alf
is recommending you ignore the throw syntax after a function prototype
as it will be dropped in future according to the C++0x standard. May I
suggest you try writing some C++ code which generates output in relevant
places to clarify your understanding of the semantics you're
investigating. Good luck with your learning of C++.

Regards

cpp4ever
 
R

RB

Thank you that pretty much clears it up for me. After I posted it I began
to think how ridiculous that seemed to be. I would be attempting to
return executable code (in whatever format). But you have illuminated
the throw aspect even more. I had never run across such and it had me
curious.
 
C

cpp4ever

I am relatively inexperienced in C++ but trying to learn. I ran across this
in my studies. My question is not about the declspec since I understand
that
it keeps the prolog and epilog out of the declared function.
My question rather is exactly what is going on with throw( ) . I.e. is
throw
returning the previous function ?
------------------------------------------------
inline __declspec(naked) unsigned __fastcall Xadd(volatile unsigned* t,
int x) throw()
{
__asm mov eax, edx
__asm lock xadd dword ptr [ecx], eax
__asm ret
}

May I suggest you might look at class functors which may provide you
with the sort of functionality you seek. Although I would recommend you
become familiar with other aspects of class semantics first.

cpp4ever
 
M

mzdude

Exception specifications are deprecated in C++0x. That doesn't mean
that their meaning has changed, nor that you shouldn't use them. It's a
warning that in some future standard they might be removed.

Which to most people means don't use them, since they could go away
at any time.
 
J

James Kanze

* (e-mail address removed), on 30.06.2010 15:41:
I am relatively inexperienced in C++ but trying to learn. I
ran across this in my studies. My question is not about the
declspec since I understand that it keeps the prolog and
epilog out of the declared function.
My question rather is exactly what is going on with
throw( ) . I.e. is throw returning the previous function ?
------------------------------------------------
inline __declspec(naked) unsigned __fastcall Xadd(volatile unsigned* t, int x) throw()
{
__asm mov eax, edx
__asm lock xadd dword ptr [ecx], eax
__asm ret
}
That's an exception spec. This spec says that the function
will not throw any exceptions. If for some reason the
function does throw an exception (or throws an exception
that is not specified in the spec), a predefined function
void unexpected() is called. The default action is to call
terminate().
Since the OP is using MSVC: MSVC just ignores exception specifications.

In which case, it's not a C++ compiler. However...

Even if VC++ normally handled exception specifications
correctly, I would guess that __declspec(naked) might suppress
this (which presumably would be handled in the function prolog
and epilog).
And in C++0x they'll be removed or at least deprecated.

And replaced by somthing else which does more or less the same
thing.
 
J

James Kanze

On 2010-06-30 10:12:42 -0400, cpp4ever said: [...]
Exception specifications are deprecated in C++0x. That doesn't
mean that their meaning has changed, nor that you shouldn't
use them. It's a warning that in some future standard they
might be removed.

If I understand correctly, something else has been introduced to
replace an empty exception specification (which is generally
acknowledged to be useful---unlike the more general form).
 
J

James Kanze

On 2010-06-30 15:57:44 -0400, mzdude said:
Well, yes, if "any time" means "maybe after publication of a
later standard". Certainly not for at least five years.

More than that, actually. I don't think that the committee can
begin considering a new version of the standard before five
years after the previous version has been published. Which
hasn't happened yet.

In theory, "deprecated" is little more than a statement of
intent. If even that. The committee *could* change any major
language feature, deprecated or not. (If I'm not mistaken, the
current draft removes "export", even though it was never
deprecated. But keeps [io]strstream, even though that was
deprecated.)

In practice, at least in other languages, deprecated features
don't disappear from the standard for at least 20 years. And
implementors don't actually remove them for a long time after
that. (I seem to recall reading that Fortran 66 deprecated
"EQUIVALENCE", but I'm willing to bet that most, if not all
Fortran compilers today still support it.) Generally speaking:
if the deprecation is because the feature wasn't generally
supported anyway, or wasn't considered useful, and there is no
good replacement, I'd avoid it. Otherwise, for things like an
empty exception specification, or auto_ptr, I wouldn't hesitate
using it in new code, since my compilers don't support the
replacement, and knowing that I'll probably have to modify it
sometime in the next fourty or fifty years.
 
O

osmium

James Kanze said:
(If I'm not mistaken, the
current draft removes "export", even though it was never
deprecated.

I can't even imagine how that could have been sensibly implemented although
I understand it has been done - I don't know about the sensibly part,
though.

The ways that occur to me sound like the cure is worse than the disease.
 

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,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top