<?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: In C++ throw is an expression</title>
	<atom:link href="http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/</link>
	<description>sin(x) = x</description>
	<lastBuildDate>Mon, 09 Jan 2012 20:46:03 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
	<item>
		<title>By: Jason Turner</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/comment-page-1/#comment-2634</link>
		<dc:creator>Jason Turner</dc:creator>
		<pubDate>Mon, 03 Aug 2009 21:12:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=384#comment-2634</guid>
		<description>&lt;a href=&quot;#comment-2619&quot; rel=&quot;nofollow&quot;&gt;@Paercebal &lt;/a&gt; I agree 110%, I do as much work as I possibly can in my initializer list and the rest in the constructor. In fact, doing as much work as you can in the constructor is a fundamental premise of C++: &lt;a href=&quot;http://blog.emptycrate.com/node/274&quot; rel=&quot;nofollow&quot;&gt;RAII&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p><a href="#comment-2619" rel="nofollow">@Paercebal </a> I agree 110%, I do as much work as I possibly can in my initializer list and the rest in the constructor. In fact, doing as much work as you can in the constructor is a fundamental premise of C++: <a href="http://blog.emptycrate.com/node/274" rel="nofollow">RAII</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yury</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/comment-page-1/#comment-2627</link>
		<dc:creator>Yury</dc:creator>
		<pubDate>Sat, 01 Aug 2009 19:01:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=384#comment-2627</guid>
		<description>&gt;&gt;It’s academic what the type of a throw expression is, since it will never be returned, but for type-checking purposes the compiler seems to be happy to use it in place of any type.

throw-expression is of type void actually (see 15/1). What happens is special treatment of throw-expression in conditional-expression as per 5.16/2. I don&#039;t think this is that academic but rather to save some typing and have:

int y = x &gt; 4 ? x : throw std::out_of_range (&quot;&quot;) ;

instead of:

int y = x &gt; 4 ? x : (throw std::out_of_range (&quot;&quot;), 0) ;

which still would be necessary if you decide to call void-returning function instead of raising an exception.</description>
		<content:encoded><![CDATA[<p>&gt;&gt;It’s academic what the type of a throw expression is, since it will never be returned, but for type-checking purposes the compiler seems to be happy to use it in place of any type.</p>
<p>throw-expression is of type void actually (see 15/1). What happens is special treatment of throw-expression in conditional-expression as per 5.16/2. I don&#8217;t think this is that academic but rather to save some typing and have:</p>
<p>int y = x &gt; 4 ? x : throw std::out_of_range (&#8220;&#8221;) ;</p>
<p>instead of:</p>
<p>int y = x &gt; 4 ? x : (throw std::out_of_range (&#8220;&#8221;), 0) ;</p>
<p>which still would be necessary if you decide to call void-returning function instead of raising an exception.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: righteous</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/comment-page-1/#comment-2624</link>
		<dc:creator>righteous</dc:creator>
		<pubDate>Sat, 01 Aug 2009 13:22:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=384#comment-2624</guid>
		<description>Good to know this. But I hardly use the throw/catch in C++, unlike in Java</description>
		<content:encoded><![CDATA[<p>Good to know this. But I hardly use the throw/catch in C++, unlike in Java</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Smallshire</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/comment-page-1/#comment-2623</link>
		<dc:creator>Robert Smallshire</dc:creator>
		<pubDate>Sat, 01 Aug 2009 07:42:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=384#comment-2623</guid>
		<description>&lt;a href=&quot;#comment-2622&quot; rel=&quot;nofollow&quot;&gt;@Paul Betts &lt;/a&gt; Very concise!  This reminds me of the construct in Perl

&lt;code&gt;open(FOO,$foo) &#124;&#124; die &quot;Can&#039;t open $foo: $!&quot;;&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p><a href="#comment-2622" rel="nofollow">@Paul Betts </a> Very concise!  This reminds me of the construct in Perl</p>
<p><code>open(FOO,$foo) || die "Can't open $foo: $!";</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Betts</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/comment-page-1/#comment-2622</link>
		<dc:creator>Paul Betts</dc:creator>
		<pubDate>Sat, 01 Aug 2009 02:24:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=384#comment-2622</guid>
		<description>You can also use this behavior for null checks

SomeClass* ptr = foobar-&gt;Baz &#124;&#124; throw &quot;Failed!&quot;;</description>
		<content:encoded><![CDATA[<p>You can also use this behavior for null checks</p>
<p>SomeClass* ptr = foobar-&gt;Baz || throw &#8220;Failed!&#8221;;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paercebal</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/comment-page-1/#comment-2619</link>
		<dc:creator>Paercebal</dc:creator>
		<pubDate>Fri, 31 Jul 2009 21:03:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=384#comment-2619</guid>
		<description>@Tom

Try this link for questions about destruction in partially constructed objects:

http://stackoverflow.com/questions/188693/is-the-destructor-called-if-the-constructor-throws-an-exception/188882

What&#039;s happen in constructors/destructor is usually easy to understand (even if it can get confused when dealing with multiple inheritance). The order of member variables construction/destruction is known (i.e. the order of their declaration in the class/struct, or its exact reverse for destruction).

The &quot;do nothing in constructor/destructor&quot; is a pattern usually seen in C coders who came to C++, and of course, it is a wrong pattern.</description>
		<content:encoded><![CDATA[<p>@Tom</p>
<p>Try this link for questions about destruction in partially constructed objects:</p>
<p><a href="http://stackoverflow.com/questions/188693/is-the-destructor-called-if-the-constructor-throws-an-exception/188882" rel="nofollow">http://stackoverflow.com/questions/188693/is-the-destructor-called-if-the-constructor-throws-an-exception/188882</a></p>
<p>What&#8217;s happen in constructors/destructor is usually easy to understand (even if it can get confused when dealing with multiple inheritance). The order of member variables construction/destruction is known (i.e. the order of their declaration in the class/struct, or its exact reverse for destruction).</p>
<p>The &#8220;do nothing in constructor/destructor&#8221; is a pattern usually seen in C coders who came to C++, and of course, it is a wrong pattern.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tom Ritchford</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/comment-page-1/#comment-2617</link>
		<dc:creator>Tom Ritchford</dc:creator>
		<pubDate>Fri, 31 Jul 2009 16:46:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=384#comment-2617</guid>
		<description>&quot;exceptions are the best way to signal constructor failure&quot;.

I do not agree (with almost 20 years of experience in the language).

Constructors and destructors are two of the most dangerous places in the language - because tons of &quot;invisible code&quot; is called, constructors of parts or of parents that&#039;s not represented in code at all but created by the compiler.

There are a lot of arcane details there, relating for example to the creation and destruction order (particularly of static objects).  Best to not even have to think about it.

As a rule, I do as absolutely little as possible in an object&#039;s constructor.  This rule usefully applies to every language I write in, it seems!  (Well... it seems less important in Python, but then everything there seems really light anyway...)

If you need to validate parameters, do that in a factory function. Factory functions have a lot of advantages over C++ constructors anyway: you can have multiple factory functions with the same signature, you can give them self-documenting names, and most important, they&#039;re easy to mock where constructors cannot be.

In general, it&#039;s a bad idea to do something that might confuse the next person without a really good reason.  When I&#039;m debugging a system that&#039;s displaying unusual behavior, if I see

RangeChecked::RangeChecked(int x) : int_member(x &gt; 0 ? x : throw std::out_of_range) {}

I immediately spend a few minutes thinking it through and probably searching the documentation.  And I&#039;d worry in the back of my head that this might not work under a different compiler.

Even a comment would require me to read it and ask myself if I really believed it.

It&#039;s a small cognitive load on each later programmer - for zero benefit.  Keep it simple!</description>
		<content:encoded><![CDATA[<p>&#8220;exceptions are the best way to signal constructor failure&#8221;.</p>
<p>I do not agree (with almost 20 years of experience in the language).</p>
<p>Constructors and destructors are two of the most dangerous places in the language &#8211; because tons of &#8220;invisible code&#8221; is called, constructors of parts or of parents that&#8217;s not represented in code at all but created by the compiler.</p>
<p>There are a lot of arcane details there, relating for example to the creation and destruction order (particularly of static objects).  Best to not even have to think about it.</p>
<p>As a rule, I do as absolutely little as possible in an object&#8217;s constructor.  This rule usefully applies to every language I write in, it seems!  (Well&#8230; it seems less important in Python, but then everything there seems really light anyway&#8230;)</p>
<p>If you need to validate parameters, do that in a factory function. Factory functions have a lot of advantages over C++ constructors anyway: you can have multiple factory functions with the same signature, you can give them self-documenting names, and most important, they&#8217;re easy to mock where constructors cannot be.</p>
<p>In general, it&#8217;s a bad idea to do something that might confuse the next person without a really good reason.  When I&#8217;m debugging a system that&#8217;s displaying unusual behavior, if I see</p>
<p>RangeChecked::RangeChecked(int x) : int_member(x &gt; 0 ? x : throw std::out_of_range) {}</p>
<p>I immediately spend a few minutes thinking it through and probably searching the documentation.  And I&#8217;d worry in the back of my head that this might not work under a different compiler.</p>
<p>Even a comment would require me to read it and ask myself if I really believed it.</p>
<p>It&#8217;s a small cognitive load on each later programmer &#8211; for zero benefit.  Keep it simple!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Robert Smallshire</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/comment-page-1/#comment-2616</link>
		<dc:creator>Robert Smallshire</dc:creator>
		<pubDate>Fri, 31 Jul 2009 13:51:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=384#comment-2616</guid>
		<description>There are some claims &lt;a href=&quot;http://www.reddit.com/r/programming/comments/96aku/in_c_throw_is_an_expression/&quot; rel=&quot;nofollow&quot;&gt;over on Reddit&lt;/a&gt;, that use of this within a constructor initializer may be bad practice.  The reason is that C++ refuses to call destructors for objects that are partially constructed.  According to More Effective C++ &quot;you must design your constructors so that they clean up after themselves. Often, this involves simply catching all possible exceptions, executing some cleanup code, the rethrowing the exception so it continues to propagate&quot;.

I disagree that exceptions shouldn&#039;t be thrown from constructors, and take the view that exceptions are the best way to signal constructor failure. Each member that has already been constructed should have its destructor called, so it is not an issue, unless the data members are unmanaged, which is usually bad practice anyway.

It doesn&#039;t make sense for the destructor to be called on a partially constructed object, which is why the destructor will never be run unless the constructor ran to completion. If any manual cleanup &lt;i&gt;is&lt;/i&gt; needed, it can be handled, even from a exception originating in the constructor initializer list, by use of a &lt;a href=&quot;http://www.ddj.com/cpp/184401297&quot; rel=&quot;nofollow&quot;&gt;function-try block&lt;/a&gt;, but at that point any concision gained by not moving checking into the constructor body and assigning to the data members has probably been lost.</description>
		<content:encoded><![CDATA[<p>There are some claims <a href="http://www.reddit.com/r/programming/comments/96aku/in_c_throw_is_an_expression/" rel="nofollow">over on Reddit</a>, that use of this within a constructor initializer may be bad practice.  The reason is that C++ refuses to call destructors for objects that are partially constructed.  According to More Effective C++ &#8220;you must design your constructors so that they clean up after themselves. Often, this involves simply catching all possible exceptions, executing some cleanup code, the rethrowing the exception so it continues to propagate&#8221;.</p>
<p>I disagree that exceptions shouldn&#8217;t be thrown from constructors, and take the view that exceptions are the best way to signal constructor failure. Each member that has already been constructed should have its destructor called, so it is not an issue, unless the data members are unmanaged, which is usually bad practice anyway.</p>
<p>It doesn&#8217;t make sense for the destructor to be called on a partially constructed object, which is why the destructor will never be run unless the constructor ran to completion. If any manual cleanup <i>is</i> needed, it can be handled, even from a exception originating in the constructor initializer list, by use of a <a href="http://www.ddj.com/cpp/184401297" rel="nofollow">function-try block</a>, but at that point any concision gained by not moving checking into the constructor body and assigning to the data members has probably been lost.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jlouis</title>
		<link>http://www.smallshire.org.uk/sufficientlysmall/2009/07/31/in-c-throw-is-an-expression/comment-page-1/#comment-2615</link>
		<dc:creator>jlouis</dc:creator>
		<pubDate>Fri, 31 Jul 2009 12:42:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.smallshire.org.uk/sufficientlysmall/?p=384#comment-2615</guid>
		<description>Most functional languages with built-in exceptions have their exception-raising construct as an expresssion. The typing of such expressions are not that hard. Basically &quot;throw e&quot; for some expression e, returns whatever type fits into the remaining expression. Ie, the throw is parametric for any type t (think, perhaps, of throw as a template instantiated with the correct return type).

Another approach is the one Haskell takes, where the throw, and the context is operating inside a monad, but it is farther away from the C++ semantics.</description>
		<content:encoded><![CDATA[<p>Most functional languages with built-in exceptions have their exception-raising construct as an expresssion. The typing of such expressions are not that hard. Basically &#8220;throw e&#8221; for some expression e, returns whatever type fits into the remaining expression. Ie, the throw is parametric for any type t (think, perhaps, of throw as a template instantiated with the correct return type).</p>
<p>Another approach is the one Haskell takes, where the throw, and the context is operating inside a monad, but it is farther away from the C++ semantics.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

