memnus: Dragon with pigtails and glasses, saying "No sense... in a way that blows your mind?" (That makes no sense! (O&M))
Brian ([personal profile] memnus) wrote2005-11-05 03:28 pm

C/C++ issues

The code I'm trying to port (UNIX->Windows) is an extreme fan of this convention:

void function()
{
  int variable, othervariable;

  int otherfunction(arguments)
  {
    doStuff(arguments);
    doDifferentStuff(variable, othervariable);
    return result;
  }

  doSomethingElse(otherfunction);
}


Visual Studio flips out and cries when this happens; the very same code compiled happily in GCC under Cygwin. I saw something somewhere that declaring functions inside other functions is strictly forbidden in C; is this true? Does GCC waive this little restriction? Is there anything else I can do about this than make the function and the variable global? I suppose I could make a struct that has those variables within it, add that as an argument to otherfunction and everything else that's ever aware of it, but that sounds like a royal pain. Is there a setting somewhere that makes this possible again?

click

[identity profile] derakon.livejournal.com 2005-11-06 12:30 am (UTC)(link)
*blink*

I've not seen that convention before. I suppose it's supposed to be a bit like anonymous functions, and the original author wishes he were working in SML or Lisp or something. Personally I'd just move the inner function out and make it a normal global function. Unless, of course, there's some reason that the code is like that which I can't discern without context and/or content.

[identity profile] memnus.livejournal.com 2005-11-06 12:34 am (UTC)(link)
Note that the inner function references variables that are local to the outer function.

[identity profile] derakon.livejournal.com 2005-11-06 01:44 am (UTC)(link)
Oh, ick. Pass them as arguments to the inner function, then.

[identity profile] kushali.livejournal.com 2005-11-06 02:06 am (UTC)(link)
Does it compile using the visual studio compiler from the command line?

This sounds odd but in C# at least some things compile from the command line that don't compile from within the product, even using the same compiler. I think the GUI has weird flags turned on by default. I'm not talking about warnings, I've seen inconsistent errors between the two methods.

Not that it helps you but it may make you feel better about thinking VS is on crack.

[identity profile] macdaddyfrosh.livejournal.com 2005-11-06 02:56 am (UTC)(link)
I seem to recall (while reading an OCaml tutorial, of all things) that this isn't strictly forbidden in some versions of C, so some compilers let you do it (because there isn't really a reason it should be impossible, so why not?).

Still, it's not good C{++}, so I'd globalize (probably as a private) that inner function as a helper, and go from there.

Of course, if you're writing in an ML or LISP variant, or Python (and presumably many others) this is good style, and actually works quite well.

[identity profile] vrable.livejournal.com 2005-11-06 05:19 am (UTC)(link)
Described in the gcc info page, under the node "Nested Functions". I don't know what the standard says, but my impression is it's fairly nonstandard/nonportable. Also, the info page notes it isn't supported by g++...

I'd probably convert it so that doSomethingElse takes an additional argument (or more) which is passed to otherFunction when it is called. (E.g., a void * argument.) Or, there are some crazy things that C++ can do with templates and classes...