Re: Type System as Design Tool [Was: We do not use C++ exceptions]
thant.tessman@gmail.com wrote:
On Feb 22, 10:54 am, Mathias Gaunard <loufo...@gmail.com> wrote:
[...]
std::bind1st, std::mem_fun etc. could be said to be forms of
curryfication, and have been there since before the standardization of
C++.
I know I'm just getting sand in the vaseline every time I try to make
the point here, but it really isn't the same thing. Imagine the
ability to define a function within a function. The inner function is
allowed to refer to any value within any enclosing scope. The outer
function is allowed to return the inner function as a value, and the
outer function can be called any number of times creating any number
of functions.
To make this work, you need what's called 'closures' which implies
garbage collection at the language level. Stack-based memory
management doesn't get you there. In C++ you can build function
objects which can do their own memory management, but these things
never really quite succeed in being natural extensions of the base
language.
You're talking about lexical closures, and you're correct that run-time
C++ doesn't have them (though compile-time C++ does). However, I'm not
sure why you don't like "function objects that do their own memory
management." I would also suggest that C++ automatic storage makes
explicit memory management largely unnecessary; for example, I routinely
write code of the following form (minus all the hard-coding, of course):
#include <algorithm>
#include <iostream>
#include <iterator>
int main() {
int a[] = { 1, 2, 3 };
transform(
a, a + 3,
std::ostream_iterator<int>( std::cout ),
bind1st(std::plus<int>( ), 5));
std::cout << '\n';
}
That example explicitly creates three functors, none of which have to be
freed manually. More to your point, bind1st returns a function object
that fills the role of a lexical closure, without implying any use of
free store (nor manual management of such memory).
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]