Advertisement

Local functions--GREAT tip!!

Started by December 13, 2000 11:51 AM
7 comments, last by Stoffel 24 years, 1 month ago
There''s a tip-of-the-day over at Flipcode right now... http://www.flipcode.com/totd/ ...called "Local Functions in C++". This is a GREAT tip. It shows how to make functions within other functions (basically a function-local function object). I knew you could make function-local types, but I never thought of using them this way. I could have used this technique many times in the past, and probably will in the future. Take a look.
A similar construct is used for local 'function objects' with the STL. But I sometimes found that Visual C++ choked on it.

Compiler Error C2918
'name' : illegal use of local type in template instantiation

There was an attempt to generate a template function (that is, a function generated from a function template) based on a local type. A template can be instantiated only using a type with external linkage.




Edited by - Kylotan on December 14, 2000 5:51:27 PM
Advertisement
C/C++ doesn''t do local functions (thats a pascaline thing), are those really functors?
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
The technique described isn''t a true local function, because it doesn''t have access to the stack frame of the function it''s declared in. However, it does have the local scope resolution desired.
Kylotan:
I haven''t used function objects much, though I''m aware of their existence. I''ve only used them when I need custom predicate objects in STL classes. However, I don''t think I''ve ever used templated function objects, so I can''t verify your compiler error. Did you get it on the latest service pack? The release version of both VC++ 5.0 and 6.0 had severe template problems that were fixed in service packs.
quote: Original post by Stoffel

Kylotan:
I haven't used function objects much, though I'm aware of their existence. I've only used them when I need custom predicate objects in STL classes. However, I don't think I've ever used templated function objects, so I can't verify your compiler error. Did you get it on the latest service pack? The release version of both VC++ 5.0 and 6.0 had severe template problems that were fixed in service packs.


I've ended up using simple function pointers instead of function objects for the problem above. It all seems to work the same, anyway, and I've never yet 'needed' a function object. I don't have the service packs, because they don't work on my version of Visual C++. Not that I can really download 90mb of stuff anyway...

Edited by - Kylotan on December 14, 2000 9:17:19 PM
Advertisement
Woohoo! Finally I can make local functions. I really missed that since changing from Delphi to C++.
Thank you Stoffel!!!

@SiCrane: You're right about the problem of having no access to the other local vars. But passing them by ref should solve this

Edited by - Wunibald on December 15, 2000 7:05:43 AM
(Computer && !M$ == Fish && !Bike)
Um, I thought you could already do that like this..

  void MyFunction()   void RecursiveFunction(int i)   {      if (i > 0) RecursiveFunction(i-1);   }{   RecursiveFunction(6);}  


Or am I mistaken?

-Chris Bennett of Dwarfsoft - Site:"The Philosophers'' Stone of Programming Alchemy" - IOL
The future of RPGs - Thanks to all the goblins over in our little Game Design Corner niche
          
Wunibald: Sure you can pass them in by reference, but depending on the number of local variables it accesses it can be very inefficient, as compared to true local variables which are implemented by passing a pointer to the activation record, and so gets access to variable arguments and local variables of the parent function.

Dwarfsoft: No, that gives you a declaration error.

This topic is closed to new replies.

Advertisement