<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: C++ Events and Delegates</title>
	<atom:link href="http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/feed/" rel="self" type="application/rss+xml" />
	<link>http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/</link>
	<description></description>
	<lastBuildDate>Tue, 27 Jul 2010 02:46:52 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: Geoff Evans</title>
		<link>http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/comment-page-1/#comment-293</link>
		<dc:creator>Geoff Evans</dc:creator>
		<pubDate>Mon, 06 Jul 2009 22:30:49 +0000</pubDate>
		<guid isPermaLink="false">http://toolssig.wordpress.com/?p=45#comment-293</guid>
		<description>&gt;I’ve been using your event code for a while, and it totally rocks. I was disappointed, however, when I was unable to compile it with GCC 4.0 on the Mac. Are there any compatibility issues that you are aware of?

Hi Will!

Yeah, its a quick fix, you just need to add the typename keyword in front of all the iterators:

typename std::vector::const_iterator itr = m_Delegates.begin();</description>
		<content:encoded><![CDATA[<p>&gt;I’ve been using your event code for a while, and it totally rocks. I was disappointed, however, when I was unable to compile it with GCC 4.0 on the Mac. Are there any compatibility issues that you are aware of?</p>
<p>Hi Will!</p>
<p>Yeah, its a quick fix, you just need to add the typename keyword in front of all the iterators:</p>
<p>typename std::vector::const_iterator itr = m_Delegates.begin();</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Will Miller</title>
		<link>http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/comment-page-1/#comment-294</link>
		<dc:creator>Will Miller</dc:creator>
		<pubDate>Mon, 06 Jul 2009 22:10:35 +0000</pubDate>
		<guid isPermaLink="false">http://toolssig.wordpress.com/?p=45#comment-294</guid>
		<description>I&#039;ve been using your event code for a while, and it totally rocks.  I was disappointed, however, when I was unable to compile it with GCC 4.0 on the Mac.  Are there any compatibility issues that you are aware of?</description>
		<content:encoded><![CDATA[<p>I&#8217;ve been using your event code for a while, and it totally rocks.  I was disappointed, however, when I was unable to compile it with GCC 4.0 on the Mac.  Are there any compatibility issues that you are aware of?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: snk_kid</title>
		<link>http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/comment-page-1/#comment-292</link>
		<dc:creator>snk_kid</dc:creator>
		<pubDate>Sun, 01 Feb 2009 01:16:16 +0000</pubDate>
		<guid isPermaLink="false">http://toolssig.wordpress.com/?p=45#comment-292</guid>
		<description>I don&#039;t think it&#039;s beyond KISSing, it&#039;s very common C++, I&#039;m probably just making it sound much  more complicated than it actually is. It&#039;s just static polymorphism of templates, just like when you have a template function and in it&#039;s definition you use the operator + or some named method, it could be any type as long as the real type used supports the operation(s) the code will compile.

This is no different and the term callable entity isn&#039;t commonly used to be honest, just a technical term to refer to something that you can invoke with the &quot;(params)&quot; syntax without refer specifically to free-functions.

Code like this seems KISSable:

bool some_custom_predicate(int lhs, int rhs) { ... }
//....

std::sort(my_ints.begin(), my_ints.end(), some_custom_predicate);

or

struct some_custom_predicate_with_state {
 // some state
 bool operator()(int lhs, int rhs) const { ... }
};
//....
std::sort(my_ints.begin(), my_ints.end(), some_custom_predicate_with_state());

std::sort has an overload for all types that support the less than operator so you don&#039;t need to use custom predicates at all. This same technique works for class templates (like std::set/map) as well but you need to be able to pass the address of a free-function in the class&#039;s constructor of-course.

Anyways I&#039;m not trying to convince you because I understand what it&#039;s like, it&#039;s the same where I work, every (games) programmer is on a different level of C++, some guys are from a C background and they usually see it all as overly complicated and potentially inefficient (and can be quite easily when not used properly/carefully) and actually I think I agree with the C guys to a certain extent! it doesn&#039;t help matters further with multiple compiler vendors and multiple implementations of the standard library with varying efficiencies and build times, you have no control.

Also these guys are all domain experts in some particular area and it takes many, many years to become an expert so knowing C++ very, very in itself isn&#039;t that important.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t think it&#8217;s beyond KISSing, it&#8217;s very common C++, I&#8217;m probably just making it sound much  more complicated than it actually is. It&#8217;s just static polymorphism of templates, just like when you have a template function and in it&#8217;s definition you use the operator + or some named method, it could be any type as long as the real type used supports the operation(s) the code will compile.</p>
<p>This is no different and the term callable entity isn&#8217;t commonly used to be honest, just a technical term to refer to something that you can invoke with the &#8220;(params)&#8221; syntax without refer specifically to free-functions.</p>
<p>Code like this seems KISSable:</p>
<p>bool some_custom_predicate(int lhs, int rhs) { &#8230; }<br />
//&#8230;.</p>
<p>std::sort(my_ints.begin(), my_ints.end(), some_custom_predicate);</p>
<p>or</p>
<p>struct some_custom_predicate_with_state {<br />
 // some state<br />
 bool operator()(int lhs, int rhs) const { &#8230; }<br />
};<br />
//&#8230;.<br />
std::sort(my_ints.begin(), my_ints.end(), some_custom_predicate_with_state());</p>
<p>std::sort has an overload for all types that support the less than operator so you don&#8217;t need to use custom predicates at all. This same technique works for class templates (like std::set/map) as well but you need to be able to pass the address of a free-function in the class&#8217;s constructor of-course.</p>
<p>Anyways I&#8217;m not trying to convince you because I understand what it&#8217;s like, it&#8217;s the same where I work, every (games) programmer is on a different level of C++, some guys are from a C background and they usually see it all as overly complicated and potentially inefficient (and can be quite easily when not used properly/carefully) and actually I think I agree with the C guys to a certain extent! it doesn&#8217;t help matters further with multiple compiler vendors and multiple implementations of the standard library with varying efficiencies and build times, you have no control.</p>
<p>Also these guys are all domain experts in some particular area and it takes many, many years to become an expert so knowing C++ very, very in itself isn&#8217;t that important.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Geoff Evans</title>
		<link>http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/comment-page-1/#comment-291</link>
		<dc:creator>Geoff Evans</dc:creator>
		<pubDate>Sun, 01 Feb 2009 00:29:23 +0000</pubDate>
		<guid isPermaLink="false">http://toolssig.wordpress.com/?p=45#comment-291</guid>
		<description>&gt; Are you sure you’ll be able to use such a thing on the console dev?

We do maintain a scratch heap on the devkit runtime to allow using standard containers and small allocations for doing runtime-support for tools tasks.  You do have to be careful that general heap usage doesn&#039;t creep into your final on-disc code.

&gt; I notice that your implementation is already using the standard library vector so I didn’t understand the comment about &quot;never bought into the implementation styles&quot;.

I am talking about the specifics of the implementation of stl and boost libs, not their usage.  Using a std container and really digging into the implementation details are separate things IMO.

That callable entity stuff is interesting, but for me it goes just a tad beyond &#039;Keep it simple stupid&#039;, if you get my drift.  Perhaps its just me being old-fashioned though :)  We have a hard enough time with our hard core engine guys with straightforward C++ (classes, function, constructors, virtual APIs, and template classes), much less getting really intricate with stl with for_each, lambdas, etc...  I think there is something to be said about keeping C++ reigned in...  Though I should shut up about that because I have written some insane template code over the years that are arguably as obtuse as these callable entities.</description>
		<content:encoded><![CDATA[<p>&gt; Are you sure you’ll be able to use such a thing on the console dev?</p>
<p>We do maintain a scratch heap on the devkit runtime to allow using standard containers and small allocations for doing runtime-support for tools tasks.  You do have to be careful that general heap usage doesn&#8217;t creep into your final on-disc code.</p>
<p>&gt; I notice that your implementation is already using the standard library vector so I didn’t understand the comment about &#8220;never bought into the implementation styles&#8221;.</p>
<p>I am talking about the specifics of the implementation of stl and boost libs, not their usage.  Using a std container and really digging into the implementation details are separate things IMO.</p>
<p>That callable entity stuff is interesting, but for me it goes just a tad beyond &#8216;Keep it simple stupid&#8217;, if you get my drift.  Perhaps its just me being old-fashioned though <img src='http://thetoolsmiths.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   We have a hard enough time with our hard core engine guys with straightforward C++ (classes, function, constructors, virtual APIs, and template classes), much less getting really intricate with stl with for_each, lambdas, etc&#8230;  I think there is something to be said about keeping C++ reigned in&#8230;  Though I should shut up about that because I have written some insane template code over the years that are arguably as obtuse as these callable entities.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: snk_kid</title>
		<link>http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/comment-page-1/#comment-290</link>
		<dc:creator>snk_kid</dc:creator>
		<pubDate>Sat, 31 Jan 2009 23:41:41 +0000</pubDate>
		<guid isPermaLink="false">http://toolssig.wordpress.com/?p=45#comment-290</guid>
		<description>Hmmm, some of my code got lost in a webpage singularity, it should have been:

&lt;CODE&gt;
template
inline void for_each_something_do(InputIterator first, InputIterator last, UnaryFunction fun)
{
	for(; first != last; ++first)
		fun(*first);
}
&lt;/CODE&gt;</description>
		<content:encoded><![CDATA[<p>Hmmm, some of my code got lost in a webpage singularity, it should have been:</p>
<p><code><br />
template<br />
inline void for_each_something_do(InputIterator first, InputIterator last, UnaryFunction fun)<br />
{<br />
	for(; first != last; ++first)<br />
		fun(*first);<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: snk_kid</title>
		<link>http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/comment-page-1/#comment-289</link>
		<dc:creator>snk_kid</dc:creator>
		<pubDate>Sat, 31 Jan 2009 23:35:34 +0000</pubDate>
		<guid isPermaLink="false">http://toolssig.wordpress.com/?p=45#comment-289</guid>
		<description>Sorry about the first post I was a bit of an ass there, boost isn&#039;t the only C++ library for signals &amp; slots library out there by the way.

Are you sure you&#039;ll be able to use such a thing on the console dev? I usually work in tools so It&#039;s alright for me to use such components but on console dev we are very strict and careful as to what we use because as I&#039;m sure you very well know we are dealing with a number of constraints &amp; variables that can really hurt performance if we are not careful and get sloppy.

Most/all components in boost are independent of each other and self-contained however they try to target many platforms and compilers so they have a relatively complicated header files preprocessor configuration to deal with the varying compiler C++ ISO standard conformance and have workarounds (if they exist).

Pretty much the majority of boost components are generated from macro and template metap-rogramming (but some components do generate static/dynamic link libraries), most likely using &lt;a href=&quot;http://www.boost.org/doc/libs/1_37_0/libs/preprocessor/doc/index.html&quot; rel=&quot;nofollow&quot;&gt;Boost.Preprocessor&lt;/a&gt; (a very useful preprocessor library and works with pure C compilers) and &lt;a href=&quot;http://www.boost.org/doc/libs/1_37_0/libs/mpl/doc/index.html&quot; rel=&quot;nofollow&quot;&gt;boost::mpl&lt;/a&gt; (template meta-programming library).

I don&#039;t know but (ignoring all the potential issues with compilers for consoles and hardware constraints) it might be a bit daunting to try and make a port but you can easily talk to the authors on the dev mailing lists. If there is something you&#039;re not sure about.

They do have an installer to select the various types of libraries (static/dynamic libraries, statically/dynamically linking to CRT and so on) to download and install, &lt;a href=&quot;http://www.boostpro.com/products/free&quot; rel=&quot;nofollow&quot;&gt;here (click me)&lt;/a&gt;.

One thing to note about boost their components have had a major influence on the next C++ ISO standard revision (C++0x) such as smart pointers and &quot;polymorphic function object wrappers&quot; (boost/std::tr1::function) and not just libraries even the language itself. This is no surprise since some of the members are part of the C++ ISO Standard committee.

Anyways back to the last question, to me it&#039;s less clear than &quot;my_signal(params);&quot;, and this is the same syntax used for .NET delegates but I do see some advantages of having a named method.

I actually said callable entity not capable lol, `callable entity` is C++ terminology for anything you can call/invoke with &quot;()&quot; syntax. It could be free-function, a bound pointer to member function, functional object. This is a major hint about generic programming and what I was talking about and the advantage of providing a functional call operator overload (you could have both methods).

I don&#039;t know if you&#039;re aware of this but you can use templates to pass in `callable entities` in your functions and not be limited to just free-functions or pointer to member functions, it&#039;s very polymorphic like but still being efficient, an example:

template
inline void for_each_something_do(InputIterator first, InputIterator last, UnaryFunction fun)
{
    for(; first != last; ++first)
	fun(*first);
}

Now you can pass any `callable entity`, even with your signals if you provide an overload of the functional call operator:

inline void print_int(int i) { std::cout &lt;&lt; i &lt;&lt; &#039;\n&#039;; }

struct int_printer {
    void operator()(int i) const { std::cout &lt;&lt; i &lt;&lt; &#039;\n&#039;; }
};

for_each_something_do(my_ints.begin(), my_ints.end(), print_int);
for_each_something_do(my_ints.begin(), my_ints.end(), int_printer());

The C++ generic algorithms (and C++ standard library) pretty much works (and is used) like this, very functional by nature.

Now you can use functional objects you can take the idea further and emulate lexical closures in C++. They can `capture the environment` and your &quot;functions&quot; can carry state. Does your framework all working with functional objects like boost signals?

Note that C++0x officially introduces lambda expressions and lexical closures which equate to the compiler generating structs with functional call operators, all on the stack.

I notice that your implementation is already using the standard library vector so I didn&#039;t understand the comment about &quot;never bought into the implementation styles&quot;.

Can I just give a little advice, please use (nested) type aliases for iterator types! instead of std::vector::iterator/const_iterator all over the place, you can even do it in function scope level, as they say apply the &lt;a href=&quot;http://en.wikipedia.org/wiki/Don%27t_repeat_yourself&quot; rel=&quot;nofollow&quot;&gt;DRY&lt;/a&gt; principle, nested type aliases is also a very big part of generic programming actually.

I&#039;m sorry if I come off as an asshole, I really don&#039;t mean to.</description>
		<content:encoded><![CDATA[<p>Sorry about the first post I was a bit of an ass there, boost isn&#8217;t the only C++ library for signals &amp; slots library out there by the way.</p>
<p>Are you sure you&#8217;ll be able to use such a thing on the console dev? I usually work in tools so It&#8217;s alright for me to use such components but on console dev we are very strict and careful as to what we use because as I&#8217;m sure you very well know we are dealing with a number of constraints &amp; variables that can really hurt performance if we are not careful and get sloppy.</p>
<p>Most/all components in boost are independent of each other and self-contained however they try to target many platforms and compilers so they have a relatively complicated header files preprocessor configuration to deal with the varying compiler C++ ISO standard conformance and have workarounds (if they exist).</p>
<p>Pretty much the majority of boost components are generated from macro and template metap-rogramming (but some components do generate static/dynamic link libraries), most likely using <a href="http://www.boost.org/doc/libs/1_37_0/libs/preprocessor/doc/index.html" rel="nofollow">Boost.Preprocessor</a> (a very useful preprocessor library and works with pure C compilers) and <a href="http://www.boost.org/doc/libs/1_37_0/libs/mpl/doc/index.html" rel="nofollow">boost::mpl</a> (template meta-programming library).</p>
<p>I don&#8217;t know but (ignoring all the potential issues with compilers for consoles and hardware constraints) it might be a bit daunting to try and make a port but you can easily talk to the authors on the dev mailing lists. If there is something you&#8217;re not sure about.</p>
<p>They do have an installer to select the various types of libraries (static/dynamic libraries, statically/dynamically linking to CRT and so on) to download and install, <a href="http://www.boostpro.com/products/free" rel="nofollow">here (click me)</a>.</p>
<p>One thing to note about boost their components have had a major influence on the next C++ ISO standard revision (C++0x) such as smart pointers and &#8220;polymorphic function object wrappers&#8221; (boost/std::tr1::function) and not just libraries even the language itself. This is no surprise since some of the members are part of the C++ ISO Standard committee.</p>
<p>Anyways back to the last question, to me it&#8217;s less clear than &#8220;my_signal(params);&#8221;, and this is the same syntax used for .NET delegates but I do see some advantages of having a named method.</p>
<p>I actually said callable entity not capable lol, `callable entity` is C++ terminology for anything you can call/invoke with &#8220;()&#8221; syntax. It could be free-function, a bound pointer to member function, functional object. This is a major hint about generic programming and what I was talking about and the advantage of providing a functional call operator overload (you could have both methods).</p>
<p>I don&#8217;t know if you&#8217;re aware of this but you can use templates to pass in `callable entities` in your functions and not be limited to just free-functions or pointer to member functions, it&#8217;s very polymorphic like but still being efficient, an example:</p>
<p>template<br />
inline void for_each_something_do(InputIterator first, InputIterator last, UnaryFunction fun)<br />
{<br />
    for(; first != last; ++first)<br />
	fun(*first);<br />
}</p>
<p>Now you can pass any `callable entity`, even with your signals if you provide an overload of the functional call operator:</p>
<p>inline void print_int(int i) { std::cout &lt;&lt; i &lt;&lt; &#8216;\n&#8217;; }</p>
<p>struct int_printer {<br />
    void operator()(int i) const { std::cout &lt;&lt; i &lt;&lt; &#8216;\n&#8217;; }<br />
};</p>
<p>for_each_something_do(my_ints.begin(), my_ints.end(), print_int);<br />
for_each_something_do(my_ints.begin(), my_ints.end(), int_printer());</p>
<p>The C++ generic algorithms (and C++ standard library) pretty much works (and is used) like this, very functional by nature.</p>
<p>Now you can use functional objects you can take the idea further and emulate lexical closures in C++. They can `capture the environment` and your &quot;functions&quot; can carry state. Does your framework all working with functional objects like boost signals?</p>
<p>Note that C++0x officially introduces lambda expressions and lexical closures which equate to the compiler generating structs with functional call operators, all on the stack.</p>
<p>I notice that your implementation is already using the standard library vector so I didn&#8217;t understand the comment about &quot;never bought into the implementation styles&quot;.</p>
<p>Can I just give a little advice, please use (nested) type aliases for iterator types! instead of std::vector::iterator/const_iterator all over the place, you can even do it in function scope level, as they say apply the <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="nofollow">DRY</a> principle, nested type aliases is also a very big part of generic programming actually.</p>
<p>I&#8217;m sorry if I come off as an asshole, I really don&#8217;t mean to.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gorlak</title>
		<link>http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/comment-page-1/#comment-288</link>
		<dc:creator>gorlak</dc:creator>
		<pubDate>Sat, 31 Jan 2009 22:11:33 +0000</pubDate>
		<guid isPermaLink="false">http://toolssig.wordpress.com/?p=45#comment-288</guid>
		<description>I didn&#039;t know about the boost::signals stuff, thanks for pointing that out.  Seems pretty similar in terms of feature set.  We use this event/delegate code on platforms we don&#039;t want to port boost to (like PS3).  Is it possible to break out parts of boost, or is it pretty much a monolithic lib?

Calling a named method to raise the event is very clear in calling code IMO.  What are some resources for generic programming techniques?  What do you mean by &#039;capable entity&#039;?  I personally have never bought into the implementation styles of stl and boost.  I am sure I don&#039;t know the trade-offs however...</description>
		<content:encoded><![CDATA[<p>I didn&#8217;t know about the boost::signals stuff, thanks for pointing that out.  Seems pretty similar in terms of feature set.  We use this event/delegate code on platforms we don&#8217;t want to port boost to (like PS3).  Is it possible to break out parts of boost, or is it pretty much a monolithic lib?</p>
<p>Calling a named method to raise the event is very clear in calling code IMO.  What are some resources for generic programming techniques?  What do you mean by &#8216;capable entity&#8217;?  I personally have never bought into the implementation styles of stl and boost.  I am sure I don&#8217;t know the trade-offs however&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: snk_kid</title>
		<link>http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/comment-page-1/#comment-287</link>
		<dc:creator>snk_kid</dc:creator>
		<pubDate>Sat, 31 Jan 2009 21:54:54 +0000</pubDate>
		<guid isPermaLink="false">http://toolssig.wordpress.com/?p=45#comment-287</guid>
		<description>Is there any particular reason you didn&#039;t use an existing signals &amp; slots library such as &lt;a href=&quot;http://www.boost.org/doc/libs/1_37_0/doc/html/signals.html&quot; rel=&quot;nofollow&quot;&gt;Boost.Signals&lt;/a&gt;? (boost has recently added a thread-safe implementation too). Using a named method to &quot;fire an event&quot; is bit ugly. It is a `callable entity` it should overload the functional call operator, be consistent with generic programming techniques.</description>
		<content:encoded><![CDATA[<p>Is there any particular reason you didn&#8217;t use an existing signals &amp; slots library such as <a href="http://www.boost.org/doc/libs/1_37_0/doc/html/signals.html" rel="nofollow">Boost.Signals</a>? (boost has recently added a thread-safe implementation too). Using a named method to &#8220;fire an event&#8221; is bit ugly. It is a `callable entity` it should overload the functional call operator, be consistent with generic programming techniques.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gorlak</title>
		<link>http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/comment-page-1/#comment-286</link>
		<dc:creator>gorlak</dc:creator>
		<pubDate>Sat, 31 Jan 2009 20:05:32 +0000</pubDate>
		<guid isPermaLink="false">http://toolssig.wordpress.com/?p=45#comment-286</guid>
		<description>This is an important question, so I wrote another post about it &lt;a href=&quot;http://toolssig.wordpress.com/2009/01/31/to-net-or-not-to-net/&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p>This is an important question, so I wrote another post about it <a href="http://toolssig.wordpress.com/2009/01/31/to-net-or-not-to-net/" rel="nofollow">here</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael</title>
		<link>http://thetoolsmiths.org/2009/01/30/c-events-and-delegates/comment-page-1/#comment-285</link>
		<dc:creator>Michael</dc:creator>
		<pubDate>Sat, 31 Jan 2009 19:03:13 +0000</pubDate>
		<guid isPermaLink="false">http://toolssig.wordpress.com/?p=45#comment-285</guid>
		<description>I&#039;m also curious about the .NET / wxWidgets decision.</description>
		<content:encoded><![CDATA[<p>I&#8217;m also curious about the .NET / wxWidgets decision.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
