Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > does varargs offer a kind of poor man's polymorphism?

Reply
Thread Tools

does varargs offer a kind of poor man's polymorphism?

 
 
Malcolm McLean
Guest
Posts: n/a
 
      12-27-2010
On Dec 17, 5:48*am, Eric Sosman <(E-Mail Removed)> wrote:
>
> * * * * func ("The second parameter points to an `enum TriplePlay'.",
> * * * * * * * &infielder);
>
> I confess, though, that I've never seen these dodges used -- not even
> by people who've previously fallen afoul of an unexpected promotion!
>
>

In high-level languages you often see this

answer = power(base = x, exponent = y)

it means that caller can miss out parameters, and doesn't have to
remember the order in which arguments are passed. It also gives a
powerful clue to the reader what they mean.
You can do the same with C varags, but I must confess I've never see
it done.

 
Reply With Quote
 
 
 
 
Mark Wooding
Guest
Posts: n/a
 
      12-27-2010
Malcolm McLean <(E-Mail Removed)> writes:

> In high-level languages you often see this
>
> answer = power(base = x, exponent = y)
>
> You can do the same with C varags, but I must confess I've never see
> it done.


The X Toolkit Intrinsics (Xt) library does something rather like this,
e.g., in XtVaCreateWidget. The Gobject system also has something
similar, in g_object_new and friends.

-- [mdw]
 
Reply With Quote
 
 
 
 
Stephen Sprunk
Guest
Posts: n/a
 
      12-27-2010
On 16 Dec 2010 19:58, pete wrote:
> No doubt every other student of C has noticed this; it's new to me.
>
> If I declare:
>
> int xlate( void *, ... );
>
> and then define xlate( ) in several different ways (maybe all
> definitions but one are #ifdef-ed out):
>
> int xlate ( char *arg1 ) { ... }
> int xlate ( int arg1, char *arg2, int arg3 ) { ... }
> int xlate ( char arg1, int *arg2 ) { ... }
>
> and omit any mention of va_list -- never mentioning it -- in every
> definition of xlate( ); and then call xlate( ) abiding by one of its
> several definitions, it seems that every compiled version of xlate( )
> works just the way I want, at least under gcc and msvc.
>
> Is this relaxed, undemanding, generous compiler behavior guaranteed
> under C99?


No; this invokes undefined behavior. One possible result of undefined
behavior may be doing exactly what you want--until it comes time to port
to another implementation or demo your "working" code to your boss/customer.

There exist implementations which pass arguments to variadic functions
on the stack but arguments to non-variadic functions in registers, which
is allowed by the Standard but will obviously break your code.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Poor, poor P&S owner learns too late... Rich Digital Photography 66 06-11-2009 04:48 AM
Special Offer....!!!! Obtian CCNA+CCNP Without Exams In 10 Days(100% Passing Gaurantee)...HURRY UP...!!!!....Limited Time Offer...!!!!!! mcsd_exams@yahoo.com Cisco 0 07-28-2006 11:29 AM
Poor reception, poor connection, and dropped signal =?Utf-8?B?dW51c3VhbHBzeWNobw==?= Wireless Networking 2 06-07-2006 12:54 AM
NZ has such poor broadband Microsoft is worried they cannot offer "Live" news.xtra.co.nz NZ Computing 29 12-03-2005 10:12 AM
Does no one else think microsoft does a poor job? =?Utf-8?B?SmVyZW15IEx1bmRncmVu?= Wireless Networking 2 11-20-2004 12:17 AM



Advertisments