<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:georss="http://www.georss.org/georss" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">
  <channel>
    <title>Nesterovsky bros - C++</title>
    <link>http://www.nesterovsky-bros.com/weblog/</link>
    <description />
    <language>en-us</language>
    <copyright>Nesterovsky bros</copyright>
    <lastBuildDate>Fri, 26 Jul 2019 16:38:11 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.12105.0</generator>
    <managingEditor>contact@nesterovsky-bros.com</managingEditor>
    <webMaster>contact@nesterovsky-bros.com</webMaster>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=861382a1-a2b8-4b39-a9f4-1f7a2148bf59</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,861382a1-a2b8-4b39-a9f4-1f7a2148bf59.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=861382a1-a2b8-4b39-a9f4-1f7a2148bf59</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We were asked to help with search service in one enterprise. We were told that their
SharePoint portal does not serve their need. Main complaints were about the quality
of search results.
</p>
        <p>
They have decided to implement external index of SharePoint content, using <a href="https://www.elastic.co/">Elastic</a>,
and expose custom search API within the enterprise.
</p>
        <p>
        </p>
        <p>
We questioned their conclusions, asked why did they think Elastic will give much better
results, asked did they try to figure out why SharePoint give no desired results.
</p>
        <p>
Answers did not convince us though we have joined the project.
</p>
        <p>
        </p>
        <p>
What do you think? Elastic did not help at all though they hoped very much that its
query language will help to rank results in a way that matched documents will be found.
After all they thought it was a problem of ranking of results.
</p>
        <p>
        </p>
        <p>
Here we have started our analysis. We took a specific document that must be found
but is never returned from search.
</p>
        <p>
        </p>
        <p>
It turned to be well known problem, at least we dealt with closely related one in
the past. There are two ingredients here:
</p>
        <ul>
          <li>
documents that have low chances to be found are PDFs;</li>
          <li>
we live in Israel, so most texts are Hebrew, which means words are written from right
to left, while some other texts from left to right. See <a href="https://en.wikipedia.org/wiki/Bi-directional_text" target="_blank">Bi-directional
text</a>.</li>
        </ul>
        <p>
Traditionally PDF documents are provided in a way that only distantly resembles logical
structure of original content. E.g., paragraphs of texts are often represented as
unrelated runs of text lines, or as set of text runs representing single words, or
independant characters. No need to say that additional complication comes from that
Hebrew text are often represented visually (from left to right, as if "hello"
would be stored as "olleh" and would be just printed from right to left).
Another common feature of PDF are custom fonts with uncanonical mappings, or images
with glyphs of letters.
</p>
        <p>
You can implement these tricks in other document formats but for some reason PDF is
only format we have seen that regularly and intensively uses these techniques.
</p>
        <p>
        </p>
        <p>
At this point we have realized that it's not a fault of a search engine to find
the document but the feature of the PDF to expose its text to a crawler in a form
that cannot be used for search. In fact, PDF cannot search by itself in such documents,
as when you try to find some text in the document opened in a pdf viewer, that you
clearly see in the document, you often find nothing.
</p>
        <p>
        </p>
        <p>
A question. What should you do in this case when no any PDF text extractor can give
you correct text but text is there when you looking at document in a pdf viewer?
</p>
        <p>
        </p>
        <p>
We decided it's time to go in the direction of image recognition. Thankfully,
nowdays it's a matter of available processing resources.
</p>
        <p>
Our goal was:
</p>
        <ol>
          <li>
Have images of each PDF page. This task is immediately solved with <a href="https://pdfbox.apache.org/" target="_blank">Apache
PDFBox (A Java PDF Library) - it's time to say this is java project.</a></li>
          <li>
Run Optical Character Recognition (OCR) over images, and get extracted texts. This
is perfectly done by <a href="https://github.com/tesseract-ocr/tesseract" target="_blank">tesseract-ocr/tesseract</a>,
and thankfully to its java wrapper <a href="https://github.com/bytedeco/javacpp-presets/tree/master/tesseract" target="_blank">bytedeco/javacpp-presets</a> we
can immediately call this C++ API from java.</li>
        </ol>
        <p>
The only small nuisance of tesseract is that it does not expose table recognition
info, but we can easily overcome it (we solved this task in the past), as along with
each text run tesseract exposes its position.
</p>
        <p>
        </p>
        <p>
What are results of the run of such program?
</p>
        <ol>
          <li>
Full success! It works with high quality of recognition. Indeed, there is no any physical
noise that impacts quality.</li>
          <li>
Slow speed - up to several seconds per recognition per page.</li>
          <li>
Scalable solution. Slow speed can be compensated by almost unlimited theoretical scalability.</li>
        </ol>
        <p>
        </p>
        <p>
So, what is the lesson we have taked from this experience?
</p>
        <p>
        </p>
        <p>
Well, you should question yourself, test and verify ideas on the ground before building
any theories that will lead you in completely wrong direction. After all people started
to realize there was no need to claim on SharePoint, to throw it, and to spend great
deal of time and money just to prove that the problem is in the different place.
</p>
        <p>
A sample source code can be found at <a href="https://github.com/nesterovsky-bros/pdf.ocr/blob/master/src/main/java/nesterovskyBros/pdf/ocr/App.java" target="_blank">App.java</a></p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=861382a1-a2b8-4b39-a9f4-1f7a2148bf59" />
      </body>
      <title>Theory vs practice</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,861382a1-a2b8-4b39-a9f4-1f7a2148bf59.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2019/07/26/TheoryVsPractice.aspx</link>
      <pubDate>Fri, 26 Jul 2019 16:38:11 GMT</pubDate>
      <description>&lt;p&gt;
We were asked to help with search service in one enterprise. We were told that their
SharePoint portal does not serve their need. Main complaints were about the quality
of search results.
&lt;/p&gt;
&lt;p&gt;
They have decided to implement external index of SharePoint content, using &lt;a href="https://www.elastic.co/"&gt;Elastic&lt;/a&gt;,
and expose custom search API within the enterprise.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
We questioned their conclusions, asked why did they think Elastic will give much better
results, asked did they try to figure out why SharePoint give no desired results.
&lt;/p&gt;
&lt;p&gt;
Answers did not convince us though we have joined the project.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
What do you think? Elastic did not help at all though they hoped very much that its
query language will help to rank results in a way that matched documents will be found.
After all they thought it was a problem of ranking of results.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Here we have started our analysis. We took a specific document that must be found
but is never returned from search.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
It turned to be well known problem, at least we dealt with closely related one in
the past. There are two ingredients here:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
documents that have low chances to be found are PDFs;&lt;/li&gt;
&lt;li&gt;
we live in Israel, so most texts are Hebrew, which means words are written from right
to left, while some other texts from left to right. See &lt;a href="https://en.wikipedia.org/wiki/Bi-directional_text" target="_blank"&gt;Bi-directional
text&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Traditionally PDF documents are provided in a way that only distantly resembles logical
structure of original content. E.g., paragraphs of texts are often represented as
unrelated runs of text lines, or as set of text runs representing single words, or
independant characters. No need to say that additional complication comes from that
Hebrew text are often represented visually (from left to right, as if &amp;quot;hello&amp;quot;
would be stored as &amp;quot;olleh&amp;quot; and would be just printed from right to left).
Another common feature of PDF are custom fonts with uncanonical mappings, or images
with glyphs of letters.
&lt;/p&gt;
&lt;p&gt;
You can implement these tricks in other document formats but for some reason PDF is
only format we have seen that regularly and intensively uses these techniques.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
At this point we have realized that it&amp;#39;s not a fault of a search engine to find
the document but the feature of the PDF to expose its text to a crawler in a form
that cannot be used for search. In fact, PDF cannot search by itself in such documents,
as when you try to find some text in the document opened in a pdf viewer, that you
clearly see in the document, you often find nothing.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
A question. What should you do in this case when no any PDF text extractor can give
you correct text but text is there when you looking at document in a pdf viewer?
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
We decided it&amp;#39;s time to go in the direction of image recognition. Thankfully,
nowdays it&amp;#39;s a matter of available processing resources.
&lt;/p&gt;
&lt;p&gt;
Our goal was:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Have images of each PDF page. This task is immediately solved with &lt;a href="https://pdfbox.apache.org/" target="_blank"&gt;Apache
PDFBox (A Java PDF Library) - it's time to say this is java project.&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
Run Optical Character Recognition (OCR) over images, and get extracted texts. This
is perfectly done by &lt;a href="https://github.com/tesseract-ocr/tesseract" target="_blank"&gt;tesseract-ocr/tesseract&lt;/a&gt;,
and thankfully to its java wrapper &lt;a href="https://github.com/bytedeco/javacpp-presets/tree/master/tesseract" target="_blank"&gt;bytedeco/javacpp-presets&lt;/a&gt; we
can immediately call this C++ API from java.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The only small nuisance of tesseract is that it does not expose table recognition
info, but we can easily overcome it (we solved this task in the past), as along with
each text run tesseract exposes its position.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
What are results of the run of such program?
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Full success! It works with high quality of recognition. Indeed, there is no any physical
noise that impacts quality.&lt;/li&gt;
&lt;li&gt;
Slow speed - up to several seconds per recognition per page.&lt;/li&gt;
&lt;li&gt;
Scalable solution. Slow speed can be compensated by almost unlimited theoretical scalability.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
So, what is the lesson we have taked from this experience?
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Well, you should question yourself, test and verify ideas on the ground before building
any theories that will lead you in completely wrong direction. After all people started
to realize there was no need to claim on SharePoint, to throw it, and to spend great
deal of time and money just to prove that the problem is in the different place.
&lt;/p&gt;
&lt;p&gt;
A sample source code can be found at &lt;a href="https://github.com/nesterovsky-bros/pdf.ocr/blob/master/src/main/java/nesterovskyBros/pdf/ocr/App.java" target="_blank"&gt;App.java&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=861382a1-a2b8-4b39-a9f4-1f7a2148bf59" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,861382a1-a2b8-4b39-a9f4-1f7a2148bf59.aspx</comments>
      <category>C++</category>
      <category>Java</category>
      <category>Thinking aloud</category>
      <category>Tips and tricks</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=8dcc4028-a6ff-48c9-99e2-72229895c1fb</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8dcc4028-a6ff-48c9-99e2-72229895c1fb.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=8dcc4028-a6ff-48c9-99e2-72229895c1fb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
For more than 25 years continues a discussion in C++ community about exceptions. In
our opinion this can only be compared with math community and their open problems
like <a href="https://en.wikipedia.org/wiki/Hilbert's_problems">Hilbert's 23 problems</a> dated
by 1900.
</p>
        <p>
In essence C++ exception discussion is about efficiency of exceptions vs status codes.
This problem is not so acute in other languages (like java or C#) because those languages
postulate different goals. 
</p>
        <p>
C++ designers have introduced a zero-overhead principle for any language feature,
which is:
</p>
        <ol>
          <li>
If you don’t use some feature you don’t pay for it.</li>
          <li>
If you do use it you cannot (reasonably) write it more efficiently by hand. 
</li>
        </ol>
        <p>
Exceptions comparing to status codes do not withstand this demand. This led to the
fragmentation of C++ comunity where many big projects and code styles ban exceptions
partially or completely.
</p>
        <p>
Make no doubt that all this time people were trying to make exceptions better, and
have found techniques to make them space and run time efficient to some extent, but
still, old plain status codes outperform both in speed (especially in predictability
of time of exception handling logic) and in code size. 
</p>
        <p>
We guess the solution is finally found after the quarter the century of discussion!
</p>
        <p>
WG paper: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r1.pdf">Zero-overhead
deterministic exceptions: Throwing values</a> by Herb Sutter. This "<i>paper
aims to extend C++’s exception model to let functions declare that they throw a statically
specified type by value. This lets the exception handling implementation be exactly
as efficient and deterministic as a local return by value, with zero dynamic or non-local
overheads.</i>"
</p>
        <p>
In other words author suggests to:
</p>
        <ul>
          <li>
extend exception model (in fact implement additional one);</li>
          <li>
make exceptions as fast and as predicable as status codes (which virtually means designate
a status code as a new exception);</li>
        </ul>
        <p>
 
</p>
        <p>
Here are author's arguments:
</p>
        <ol>
          <li>
Status code is usually just a number, and handling an error is like to perform some <code>if</code> or <code>switch</code> statements.</li>
          <li>
Handling errors with status codes is predicable in time and visible in the code though
it burdens the logic very much.</li>
          <li>
Exceptions give clear separation between a control flow and error handling.</li>
          <li>
Exceptions are mostly invisible in the code, so it's not always clear how much
they add to code size and how they impact performance.</li>
          <li>
Statistics show that exceptions add 15 to 30% to size of code, even if no exceptions
are used (violation of zero-overhead principle);</li>
          <li>
Exceptions require Run Time Type Information about types, and have no predictable
memory (stack or heap) footprint  (violation of zero-overhead principle).</li>
        </ol>
        <p>
 
</p>
        <p>
What aurhor suggests might seem too radical but at present it's only viable solution
to reestablish zero-verhead principle and to reunite two C++ camps.
</p>
        <p>
Here is the approach.
</p>
        <ol>
          <li>
Clarify what should be considered as an exception. 
<ol><li>
Contract violation. 
<p>
Are contract violation like invalid values of arguments or invalid post conditions
(unhold invariants) are exceptions or programmer's bugs?
</p><p>
If later then it's best to terminate, as you cannot correctly recover from bug.
</p></li><li>
Virtual Machine fault. 
<p>
What user program can do with stack overflow?
</p><p>
The best according to the author it to terminate.
</p></li><li>
OOM - Out Of Memory error. 
<p>
What is the best way to deal with OOM dyring dynamic allocation.
</p><p>
At present there are two operators:
</p><ul><li><code>new</code> - to allocate memory dynamically and throw <code>bad_alloc</code> on
failure.</li><li><code>new(nothrow)</code> - to allocate memory dynamically and return <code>nullptr</code> on
failure.</li></ul><p>
Herb Sutter suggests to change <code>new</code> behavior to terminate on failure (it
is very hard to properly handle <code>bad_alloc</code> anyway), while <code>new(nothrow)</code> will
still allow to build code that can deal with OOM.
</p></li><li>
Partial success 
<p>
This should never be reported as an error, and status codes should be used to report
the state.
</p></li><li>
Error condition distinct from any type of success. 
<p>
This is where exceptions should be used.
</p></li></ol><p>
Statistics shows that with such separation more than 90% of what curently is an exception
will not be exception any more, so no any hidden exception logic is required: program
either works or terminates.
</p></li>
          <li>
Refactor exception 
<p>
Redefine what exception object is and how it is propagated.
</p><p>
It should be thin value type. At minimum it needs to contain an error code. Suggested
size is up to a couple of pointers.
</p><p>
Compiler should be able to cheaply allocate and copy it on the stack or even in the
processor's registers.
</p><p>
Such simple exception type resolves problems with lifetime of exception object, and
makes exception handling as light as checking status codes.
</p><p>
Exception should be propagated through return chanel, so it's like a new calling
convention that defines either function result or error outcome.
</p></li>
        </ol>
        <p>
        </p>
        <p>
It's not our intention to quote whole the paper here. If you're interested then please
read it. Here we want to outline our concerns.
</p>
        <ol>
          <li>
Exception payload. 
<p>
This paper emphasizes that exception type should be small.
</p><p>
So, what to do with exception payload, if any (at least error message if it's
not a static text)?
</p><p>
If this won't be resolved then developers will start to invent custom mechanisms
like GetLastErrorMessage().
</p><p>
And what to do with aggregate exceptions?
</p><p>
We think this must be clearly addressed.
</p></li>
          <li>
Implemntation shift. 
<p>
We can accept that most of the current exceptions will terminate.
</p><p>
But consider now some container that serves requests, like web container or database.
</p><p>
It may be built from multiple components and serve multiple requests concurently.
If one request will terminate we don't want for container to terminate.
</p><p>
If terminate handler is called then we cannot rely on state of the application. At
least we can expect heap leaks and un-released resources.
</p><p>
So, we either want to be able release heap and other resources per request, or we
want to go down with whole process and let OS deal with it.
</p><p>
In the later case we need to design such containers differently: as a set of cooperative
processes; OS should allow to spin processes more easily.
</p></li>
          <li>
VM with exceptions 
<p>
There are Virtual Machines that allow exception to be thrown on each instruction (like
JVM, or CLI).
</p><p>
You cannot tell in this case that code would never throw exception, as it can out
of the blue!
</p><p>
Event in x86 you can have PAGE FAULT on memory access, which can be translated into
an exception.
</p><p>
So, it's still a question whether the terminate() solution is sound in every case,
and whether compiler can optimize out exception handling if it proves staticlly that
no exception should be thrown.
</p></li>
        </ol>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=8dcc4028-a6ff-48c9-99e2-72229895c1fb" />
      </body>
      <title>C++ Exceptions</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,8dcc4028-a6ff-48c9-99e2-72229895c1fb.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2018/08/06/CExceptions.aspx</link>
      <pubDate>Mon, 06 Aug 2018 12:00:10 GMT</pubDate>
      <description>  &lt;p&gt;
For more than 25 years continues a discussion in C++ community about exceptions. In
our opinion this can only be compared with math community and their open problems
like &lt;a href="https://en.wikipedia.org/wiki/Hilbert's_problems"&gt;Hilbert&amp;#39;s 23 problems&lt;/a&gt; dated
by 1900.
&lt;/p&gt;
&lt;p&gt;
In essence C++ exception discussion is about efficiency of exceptions vs status codes.
This problem is not so acute in other languages (like java or C#) because those languages
postulate different goals. 
&lt;/p&gt;
&lt;p&gt;
C++ designers have introduced a zero-overhead principle for any language feature,
which is:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
If you don’t use some feature you don’t pay for it.&lt;/li&gt;
&lt;li&gt;
If you do use it you cannot (reasonably) write it more efficiently by hand. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Exceptions comparing to status codes do not withstand this demand. This led to the
fragmentation of C++ comunity where many big projects and code styles ban exceptions
partially or completely.
&lt;/p&gt;
&lt;p&gt;
Make no doubt that all this time people were trying to make exceptions better, and
have found techniques to make them space and run time efficient to some extent, but
still, old plain status codes outperform both in speed (especially in predictability
of time of exception handling logic) and in code size. 
&lt;/p&gt;
&lt;p&gt;
We guess the solution is finally found after the quarter the century of discussion!
&lt;/p&gt;
&lt;p&gt;
WG paper: &lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r1.pdf"&gt;Zero-overhead
deterministic exceptions: Throwing values&lt;/a&gt; by Herb Sutter. This &amp;quot;&lt;i&gt;paper
aims to extend C++’s exception model to let functions declare that they throw a statically
specified type by value. This lets the exception handling implementation be exactly
as efficient and deterministic as a local return by value, with zero dynamic or non-local
overheads.&lt;/i&gt;&amp;quot;
&lt;/p&gt;
&lt;p&gt;
In other words author suggests to:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
extend exception model (in fact implement additional one);&lt;/li&gt;
&lt;li&gt;
make exceptions as fast and as predicable as status codes (which virtually means designate
a status code as a new exception);&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Here are author&amp;#39;s arguments:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Status code is usually just a number, and handling an error is like to perform some &lt;code&gt;if&lt;/code&gt; or &lt;code&gt;switch&lt;/code&gt; statements.&lt;/li&gt;
&lt;li&gt;
Handling errors with status codes is predicable in time and visible in the code though
it burdens the logic very much.&lt;/li&gt;
&lt;li&gt;
Exceptions give clear separation between a control flow and error handling.&lt;/li&gt;
&lt;li&gt;
Exceptions are mostly invisible in the code, so it&amp;#39;s not always clear how much
they add to code size and how they impact performance.&lt;/li&gt;
&lt;li&gt;
Statistics show that exceptions add 15 to 30% to size of code, even if no exceptions
are used (violation of zero-overhead principle);&lt;/li&gt;
&lt;li&gt;
Exceptions require Run Time Type Information about types, and have no predictable
memory (stack or heap) footprint&amp;nbsp; (violation of zero-overhead principle).&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
What aurhor suggests might seem too radical but at present it&amp;#39;s only viable solution
to reestablish zero-verhead principle and to reunite two C++ camps.
&lt;/p&gt;
&lt;p&gt;
Here is the approach.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Clarify what should be considered as an exception. 
&lt;ol&gt;
&lt;li&gt;
Contract violation. 
&lt;p&gt;
Are contract violation like invalid values of arguments or invalid post conditions
(unhold invariants) are exceptions or programmer&amp;#39;s bugs?
&lt;/p&gt;
&lt;p&gt;
If later then it&amp;#39;s best to terminate, as you cannot correctly recover from bug.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
Virtual Machine fault. 
&lt;p&gt;
What user program can do with stack overflow?
&lt;/p&gt;
&lt;p&gt;
The best according to the author it to terminate.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
OOM - Out Of Memory error. 
&lt;p&gt;
What is the best way to deal with OOM dyring dynamic allocation.
&lt;/p&gt;
&lt;p&gt;
At present there are two operators:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;new&lt;/code&gt; - to allocate memory dynamically and throw &lt;code&gt;bad_alloc&lt;/code&gt; on
failure.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;new(nothrow)&lt;/code&gt; - to allocate memory dynamically and return &lt;code&gt;nullptr&lt;/code&gt; on
failure.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Herb Sutter suggests to change &lt;code&gt;new&lt;/code&gt; behavior to terminate on failure (it
is very hard to properly handle &lt;code&gt;bad_alloc&lt;/code&gt; anyway), while &lt;code&gt;new(nothrow)&lt;/code&gt; will
still allow to build code that can deal with OOM.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
Partial success 
&lt;p&gt;
This should never be reported as an error, and status codes should be used to report
the state.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
Error condition distinct from any type of success. 
&lt;p&gt;
This is where exceptions should be used.
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
Statistics shows that with such separation more than 90% of what curently is an exception
will not be exception any more, so no any hidden exception logic is required: program
either works or terminates.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
Refactor exception 
&lt;p&gt;
Redefine what exception object is and how it is propagated.
&lt;/p&gt;
&lt;p&gt;
It should be thin value type. At minimum it needs to contain an error code. Suggested
size is up to a couple of pointers.
&lt;/p&gt;
&lt;p&gt;
Compiler should be able to cheaply allocate and copy it on the stack or even in the
processor&amp;#39;s registers.
&lt;/p&gt;
&lt;p&gt;
Such simple exception type resolves problems with lifetime of exception object, and
makes exception handling as light as checking status codes.
&lt;/p&gt;
&lt;p&gt;
Exception should be propagated through return chanel, so it&amp;#39;s like a new calling
convention that defines either function result or error outcome.
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
It's not our intention to quote whole the paper here. If you're interested then please
read it. Here we want to outline our concerns.
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Exception payload. 
&lt;p&gt;
This paper emphasizes that exception type should be small.
&lt;/p&gt;
&lt;p&gt;
So, what to do with exception payload, if any (at least error message if it&amp;#39;s
not a static text)?
&lt;/p&gt;
&lt;p&gt;
If this won&amp;#39;t be resolved then developers will start to invent custom mechanisms
like GetLastErrorMessage().
&lt;/p&gt;
&lt;p&gt;
And what to do with aggregate exceptions?
&lt;/p&gt;
&lt;p&gt;
We think this must be clearly addressed.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
Implemntation shift. 
&lt;p&gt;
We can accept that most of the current exceptions will terminate.
&lt;/p&gt;
&lt;p&gt;
But consider now some container that serves requests, like web container or database.
&lt;/p&gt;
&lt;p&gt;
It may be built from multiple components and serve multiple requests concurently.
If one request will terminate we don&amp;#39;t want for container to terminate.
&lt;/p&gt;
&lt;p&gt;
If terminate handler is called then we cannot rely on state of the application. At
least we can expect heap leaks and un-released resources.
&lt;/p&gt;
&lt;p&gt;
So, we either want to be able release heap and other resources per request, or we
want to go down with whole process and let OS deal with it.
&lt;/p&gt;
&lt;p&gt;
In the later case we need to design such containers differently: as a set of cooperative
processes; OS should allow to spin processes more easily.
&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
VM with exceptions 
&lt;p&gt;
There are Virtual Machines that allow exception to be thrown on each instruction (like
JVM, or CLI).
&lt;/p&gt;
&lt;p&gt;
You cannot tell in this case that code would never throw exception, as it can out
of the blue!
&lt;/p&gt;
&lt;p&gt;
Event in x86 you can have PAGE FAULT on memory access, which can be translated into
an exception.
&lt;/p&gt;
&lt;p&gt;
So, it&amp;#39;s still a question whether the terminate() solution is sound in every case,
and whether compiler can optimize out exception handling if it proves staticlly that
no exception should be thrown.
&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=8dcc4028-a6ff-48c9-99e2-72229895c1fb" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,8dcc4028-a6ff-48c9-99e2-72229895c1fb.aspx</comments>
      <category>C++</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=41fb6e80-8184-404b-aefe-2123225a72a0</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,41fb6e80-8184-404b-aefe-2123225a72a0.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=41fb6e80-8184-404b-aefe-2123225a72a0</wfw:commentRss>
      <title>Dependency Injection</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,41fb6e80-8184-404b-aefe-2123225a72a0.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2016/10/06/DependencyInjection.aspx</link>
      <pubDate>Thu, 06 Oct 2016 11:27:42 GMT</pubDate>
      <description>  &lt;p&gt;
Our genuine love is C++. Unfortunately clients don&amp;#39;t always share our favors,
so we mostly occupied in the C#, java and javascript. Nevertheless, we&amp;#39;re closely
watching the evolution of the C++. It became more mature in the latest specs.
&lt;/p&gt;
&lt;p&gt;
Recently, we wondered how would we deal with dependency injection in C++. What we
found is only strengthened our commitment to C++.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://en.cppreference.com/w/cpp/language/parameter_pack"&gt;Parameter packs&lt;/a&gt; introduced
in C++ 11 allow trivial implementation of constructor injection, while &lt;a href="http://en.cppreference.com/w/cpp/types/type_index"&gt;std::type_index&lt;/a&gt;, &lt;a href="http://en.cppreference.com/w/cpp/types/type_info"&gt;std::type_info&lt;/a&gt; and &lt;a href="http://en.cppreference.com/w/cpp/utility/any"&gt;std:any&lt;/a&gt; give
service containers. 
&lt;/p&gt;
&lt;p&gt;
In fact there are many DI implementations out there. The one we refer here is &lt;a href="http://boost-experimental.github.io/di/index.html"&gt;Boost.DI&lt;/a&gt;.
It&amp;#39;s not standard nor we can claim it&amp;#39;s the best but it&amp;#39;s good example
of how this concept can be implemented.
&lt;/p&gt;
&lt;p&gt;
So, consider &lt;a href="http://boost-experimental.github.io/di/tutorial/index.html"&gt;their
example&lt;/a&gt; seen in Java with CDI, in C# in .NET Core injection, and in C++:
&lt;/p&gt;
&lt;p&gt;
Java:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre style="white-space: pre-wrap"&gt;
@Dependent
public class Renderer 
{
  @Inject @Device
  private int device;
};

@Dependent
public class View 
{
  @Inject @Title
  private String title;
  @Inject
  private Renderer renderer;
};

@Dependent
public class Model {};

@Dependent
public class Controller 
{
  @Inject
  private Model model;
  @Inject
  private View view;
};

@Dependent
public class User {};

@Dependent
public class App 
{
  @Inject
  private Controller controller;
  @Inject
  private User user;
};

...
  Privider&amp;lt;App&gt; provider = ...

  App app = provider.get();
&lt;/pre&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
C#:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre style="white-space: pre-wrap"&gt;public class RenderedOptions
{
  public int Device { get; set; }
}
    
public class ViewOptions
{
  public int Title { get; set; }
}
    
public class Renderer 
{
  public Renderer(IOptions&amp;lt;RendererOptions&gt; options)
  {
    Device = options.Device;
  }

  public int Device { get; set; }
}

public class View 
{
  public View(IOptions&amp;lt;ViewOptions&gt; options, Renderer renderer)
  {
    Title = options.Title;
    Renderer = renderer;
  }

  public string Title { get; set; }
  public Renderer Renderer { get; set; }
}

public class Model {}

public class Controller 
{
  public Controller(Model model, View view) 
  {
    Model = model;
    View = view;
  }

  public Model Model { get; set; }
  public View View { get; set; }
};

public class User {};

public class App 
{
  public App(Controller controller, User user) 
  {
    Controller = controller;
    User = user;
  }

  public Controller Controller { get; set; }
  public User User { get; set; }
};

...
  IServiceProvider serviceProvider = ...

  serviceProvider.GetService&amp;lt;App&gt;();
&lt;/pre&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
C++:
&lt;/p&gt;
&lt;blockquote&gt;&lt;pre style="white-space: pre-wrap"&gt;#include &amp;lt;boost/di.hpp&gt;

namespace di = boost::di;

struct renderer 
{
  int device;
};

class view 
{
public:
  view(std::string title, const renderer&amp;) {}
};

class model {};

class controller 
{
public:
  controller(model&amp;, view&amp;) {}
};

class user {};

class app 
{
public:
  app(controller&amp;, user&amp;) {}
};

int main()
{
  /**
   * renderer renderer_;
   * view view_{"", renderer_};
   * model model_;
   * controller controller_{model_, view_};
   * user user_;
   * app app_{controller_, user_};
   */

  auto injector = di::make_injector();
  injector.create&amp;lt;app&gt;();
}
&lt;/pre&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
What is different between these DI flavors?
&lt;/p&gt;
&lt;p&gt;
Not too much from the perspective of the final task achieved.
&lt;/p&gt;
&lt;p&gt;
In java we used &lt;a href="http://docs.jboss.org/weld/reference/2.4.0.CR1/en-US/html/injection.html"&gt;member
injection&lt;/a&gt;, with &lt;a href="http://docs.jboss.org/weld/reference/2.4.0.CR1/en-US/html/injection.html#_qualifier_annotations"&gt;qualifiers&lt;/a&gt; to
inject scalars.
&lt;/p&gt;
&lt;p&gt;
In C# we used &lt;a href="https://docs.asp.net/en/latest/fundamentals/dependency-injection.html"&gt;constructor
injection&lt;/a&gt; with &lt;a href="https://docs.asp.net/en/latest/fundamentals/configuration.html#options-config-objects"&gt;Options
pattern&lt;/a&gt; to inject scalars.
&lt;/p&gt;
&lt;p&gt;
In C++ we used constructor injection with direct constants injected.
&lt;/p&gt;
&lt;p&gt;
All technologies have their API to initialize DI container, but, again, while API
is different, the idea is the same.
&lt;/p&gt;
&lt;p&gt;
So, expressiveness of C++ matches to those of java and C#.
&lt;/p&gt;
&lt;p&gt;
Deeper analysis shows that java&amp;#39;s CDI is more feature rich than DI of C# and C++,
but, personally, we consider it&amp;#39;s advantage of C# and C++ that they have such
a light DI.
&lt;/p&gt;
&lt;p&gt;
At the same time there is an important difference between C++ vs java and C#.
&lt;/p&gt;
&lt;p&gt;
While both java and C# are deemed to use reflection (C# in theory could use code generation
on the fly to avoid reflection), C++&amp;#39;s DI natively constructs and injects services.
&lt;/p&gt;
&lt;p&gt;
What does it mean for the user?
&lt;/p&gt;
&lt;p&gt;
Well, a lot! Both in java and in C# you would not want to use DI in a performance
critical part of code (e.g. in a tight loop), while it&amp;#39;s Ok in C++ due to near
to zero performance impact from DI. This may result in more modular and performant
code in C++.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=41fb6e80-8184-404b-aefe-2123225a72a0" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,41fb6e80-8184-404b-aefe-2123225a72a0.aspx</comments>
      <category>.NET</category>
      <category>C++</category>
      <category>Java</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=0b757059-c123-4997-83e2-eb04d976edcb</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,0b757059-c123-4997-83e2-eb04d976edcb.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=0b757059-c123-4997-83e2-eb04d976edcb</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Among latest C++ proposals the most ambiguous is <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4021.pdf">N4021</a>. 
</p>
        <p>
The goal of that proposal is <span style="font-style: italic">"to define a 2D drawing
API for the C++ programming language".</span></p>
        <p>
The motivation is going like this:
</p>
        <p style="padding-left: 1em; font-style: italic">
Today, computer graphics are pervasive in modern life, and are even replacing console-style
I/O for basic user interaction on many platforms. For example, a simple <code>cout
&lt;&lt; "Hello, world!"</code> statement doesn’t do anything useful on many tablets
and smartphones. We feel that C++ programmers should have a simple, standard way of
displaying 2D graphics to users.
</p>
        <p>
Authors compare several public and proprietary APIs to select the one named <a href="http://cairographics.org/">cairo
graphics library</a> as a base. 
</p>
        <p>
Reflecting on starting point they write:
</p>
        <p style="padding-left: 1em; font-style: italic">
Taken as a whole, starting from cairo allows for the creation of a 2D C++ drawing
library that is already known to be portable, implementable, and useful without the
need to spend years drafting, implementing, and testing a library to make sure that
it meets those criteria.<br />
...<br />
An alternative design would be to <a href="http://xkcd.com/927/">create a new API
via a synthesis of existing 2D APIs</a>. This has the benefit of being able to avoid
any perceived design flaws that existing APIs suffer from. Unfortunately this would
not have implementation and usage experience. Further, doing so would not provide
any guarantee that design flaws would not creep in.
</p>
        <p>
What follows is a discussion on best way to transform that C library into std style
C++ API.
</p>
        <p>
 
</p>
        <p>
Our thoughts on this proposal are threefold:
</p>
        <ol>
          <li>
This proposal seems a decade or two late.</li>
          <li>
C++ standard should be modular to support basic and optional features.</li>
          <li>
We feel that programmers will not be satisfied with bare 2D graphics. It's not
enough at nowadays.</li>
        </ol>
        <p>
 
</p>
        <p>
Indeed, appeals to create standard C++ API for UI are as old as the C++'s standardization
process. It's clear why did the committee not produce such API yet: they are bureaucracy
that can approve API only. In fact it's a role of community to invent and implement
libraries that may make their way into the standard. Without consensus in community
no standard will reflect such API.
</p>
        <p>
On the other hand C++ spec at present is too fat. Probably, not many people are satisfied
with the pace of its evolution. Any big chunk of a new API makes the progress even
slower. C++ spec should go through a refactoring and be split into core(s) and libraries
and to allow individual progress of each part. This will simplify both specification
and implementation. After that refactoring an API can be added or deprecated much
more easily. In fact implementations were always like this. It's the spec that
tries to be monolith.
</p>
        <p>
As for a new 2D graphics API. It looks like an idea from late 90-es. We think that
today's programmers (at least several samples <img alt=":-)" src="http://www.nesterovsky-bros.com/weblog/smilies/happy.gif" /> )
wished to deal with industry standard UI API, and not to start from basic drawing.
Looking around we observe that html 5 is such de-facto standard. Take into an account
that it supports rich layout, svg, canvas, user input; in addition it's good for
GPU optimization. Even if you want to deal with simple graphics then you can build
svg markup or draw on the canvas.
</p>
        <p>
So, what we rather prefer to see in the C++ spec is an html binding API (both for
DOM and Javascript).
</p>
        <p>
Just think of <strong>standard C++ program</strong> that uses html engine as its UI!
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=0b757059-c123-4997-83e2-eb04d976edcb" />
      </body>
      <title>A Proposal to Add 2D Graphics Rendering and Display to C++</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,0b757059-c123-4997-83e2-eb04d976edcb.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2014/08/17/AProposalToAdd2DGraphicsRenderingAndDisplayToC.aspx</link>
      <pubDate>Sun, 17 Aug 2014 08:56:08 GMT</pubDate>
      <description>  &lt;p&gt;
Among latest C++ proposals the most ambiguous is &lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4021.pdf"&gt;N4021&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
The goal of that proposal is &lt;span style="font-style: italic"&gt;"to define a 2D drawing
API for the C++ programming language".&lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
The motivation is going like this:
&lt;/p&gt;
&lt;p style="padding-left: 1em; font-style: italic"&gt;
Today, computer graphics are pervasive in modern life, and are even replacing console-style
I/O for basic user interaction on many platforms. For example, a simple &lt;code&gt;cout
&amp;lt;&amp;lt; "Hello, world!"&lt;/code&gt; statement doesn’t do anything useful on many tablets
and smartphones. We feel that C++ programmers should have a simple, standard way of
displaying 2D graphics to users.
&lt;/p&gt;
&lt;p&gt;
Authors compare several public and proprietary APIs to select the one named &lt;a href="http://cairographics.org/"&gt;cairo
graphics library&lt;/a&gt; as a base. 
&lt;/p&gt;
&lt;p&gt;
Reflecting on starting point they write:
&lt;/p&gt;
&lt;p style="padding-left: 1em; font-style: italic"&gt;
Taken as a whole, starting from cairo allows for the creation of a 2D C++ drawing
library that is already known to be portable, implementable, and useful without the
need to spend years drafting, implementing, and testing a library to make sure that
it meets those criteria.&lt;br /&gt;
...&lt;br /&gt;
An alternative design would be to &lt;a href="http://xkcd.com/927/"&gt;create a new API
via a synthesis of existing 2D APIs&lt;/a&gt;. This has the benefit of being able to avoid
any perceived design flaws that existing APIs suffer from. Unfortunately this would
not have implementation and usage experience. Further, doing so would not provide
any guarantee that design flaws would not creep in.
&lt;/p&gt;
&lt;p&gt;
What follows is a discussion on best way to transform that C library into std style
C++ API.
&lt;/p&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Our thoughts on this proposal are threefold:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
This proposal seems a decade or two late.&lt;/li&gt;
&lt;li&gt;
C++ standard should be modular to support basic and optional features.&lt;/li&gt;
&lt;li&gt;
We feel that programmers will not be satisfied with bare 2D graphics. It&amp;#39;s not
enough at nowadays.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
&amp;nbsp;
&lt;/p&gt;
&lt;p&gt;
Indeed, appeals to create standard C++ API for UI are as old as the C++&amp;#39;s standardization
process. It&amp;#39;s clear why did the committee not produce such API yet: they are bureaucracy
that can approve API only. In fact it&amp;#39;s a role of community to invent and implement
libraries that may make their way into the standard. Without consensus in community
no standard will reflect such API.
&lt;/p&gt;
&lt;p&gt;
On the other hand C++ spec at present is too fat. Probably, not many people are satisfied
with the pace of its evolution. Any big chunk of a new API makes the progress even
slower. C++ spec should go through a refactoring and be split into core(s) and libraries
and to allow individual progress of each part. This will simplify both specification
and implementation. After that refactoring an API can be added or deprecated much
more easily. In fact implementations were always like this. It&amp;#39;s the spec that
tries to be monolith.
&lt;/p&gt;
&lt;p&gt;
As for a new 2D graphics API. It looks like an idea from late 90-es. We think that
today&amp;#39;s programmers (at least several samples &lt;img alt=":-)" src="http://www.nesterovsky-bros.com/weblog/smilies/happy.gif"&gt; )
wished to deal with industry standard UI API, and not to start from basic drawing.
Looking around we observe that html 5 is such de-facto standard. Take into an account
that it supports rich layout, svg, canvas, user input; in addition it&amp;#39;s good for
GPU optimization. Even if you want to deal with simple graphics then you can build
svg markup or draw on the canvas.
&lt;/p&gt;
&lt;p&gt;
So, what we rather prefer to see in the C++ spec is an html binding API (both for
DOM and Javascript).
&lt;/p&gt;
&lt;p&gt;
Just think of &lt;strong&gt;standard C++ program&lt;/strong&gt; that uses html engine as its UI!
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=0b757059-c123-4997-83e2-eb04d976edcb" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,0b757059-c123-4997-83e2-eb04d976edcb.aspx</comments>
      <category>C++</category>
      <category>Thinking aloud</category>
    </item>
    <item>
      <trackback:ping>http://www.nesterovsky-bros.com/weblog/Trackback.aspx?guid=a9c55d27-d353-4bcf-b923-2b7f00768d22</trackback:ping>
      <pingback:server>http://www.nesterovsky-bros.com/weblog/pingback.aspx</pingback:server>
      <pingback:target>http://www.nesterovsky-bros.com/weblog/PermaLink,guid,a9c55d27-d353-4bcf-b923-2b7f00768d22.aspx</pingback:target>
      <dc:creator>Vladimir Nesterovsky</dc:creator>
      <georss:point>0 0</georss:point>
      <wfw:comment>http://www.nesterovsky-bros.com/weblog/CommentView,guid,a9c55d27-d353-4bcf-b923-2b7f00768d22.aspx</wfw:comment>
      <wfw:commentRss>http://www.nesterovsky-bros.com/weblog/SyndicationService.asmx/GetEntryCommentsRss?guid=a9c55d27-d353-4bcf-b923-2b7f00768d22</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
After C++11 revision has been approved a new cycle of C++ design has begun:
</p>
        <p style="padding-left: 1em; font-style: italic">
          <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3370.html">N3370</a>:
The C++ standards committee is soliciting proposals for additional library components.
Such proposals can range from small (addition of a single signature to an existing
library) to large (something bigger than any current standard library component). 
</p>
        <p>
At this stage it's interesting to read papers, as authors try to express ideas
rather than to formulate sentences that should go into spec as it lately was.
</p>
        <p>
These are just several papers that we've found interesting:
</p>
        <table border="1">
          <tr>
            <td>
              <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3322.pdf">N3322</a>
            </td>
            <td>
12-0012 
</td>
            <td>
A Preliminary Proposal for a Static if 
</td>
            <td>
Walter E. Brown 
</td>
          </tr>
          <tr>
            <td>
              <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3329.pdf">N3329</a>
            </td>
            <td>
12-0019 
</td>
            <td>
Proposal: static if declaration 
</td>
            <td>
H. Sutter, W. Bright, A. Alexandrescu 
</td>
          </tr>
        </table>
        <p>
Those proposals argue about compile time "if statement". The feature can
replace <code>#if</code> preprocessor directive, a SFINAE or in some cases template
specializations.
</p>
        <p>
A <code>static if</code> declaration can appear wherever a declaration or a statement
is legal. Authors also propose to add <code>static if</code> clause to a class and
a function declarations to conditionally exclude them from the scope.
</p>
        <p>
Examples:
</p>
        <p style="padding-left: 1em">
          <code>// Compile time factorial.<br />
template &lt;unsigned n&gt;<br />
struct factorial<br />
{<br />
static if (n &lt;= 1)<br />
{<br />
enum : unsigned { value = 1 };<br />
} 
<br />
else 
<br />
{<br />
enum : unsigned { value = factorial&lt;n - 1&gt;::value * n };<br />
}<br />
};<br /><br />
// Declare class provided a condition is true.<br />
class Internals if (sizeof(void*) == sizeof(int));</code>
        </p>
        <p>
Paper presents strong rationale why this addition helps to build better programs,
however the questions arise about relations between <code>static if</code> and concepts, <code>static
if</code> clause and an error diagnostics.
</p>
        <p>
        </p>
        <table border="1">
          <tr>
            <td>
              <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3327.pdf"> N3327</a>
            </td>
            <td>
12-0017</td>
            <td>
A Standard Programmatic Interface for Asynchronous Operations</td>
            <td>
N. Gustafsson, A. Laksberg</td>
          </tr>
          <tr>
            <td>
              <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3328.pdf"> N3328</a>
            </td>
            <td>
12-0018</td>
            <td>
Resumable Functions</td>
            <td>
Niklas Gustafsson</td>
          </tr>
        </table>
        <p>
That's our favorite.
</p>
        <p>
Authors propose an API and a language extensions to make asynchronous programs simpler. 
</p>
        <p>
In fact, asynchronous function will look very mush as a regular one but with small
additions. It's similar to <code>yield return</code> in C# (<span style="font-style: italic">a
construct that has been available in C# for many years and is well vetted</span>),
and to <code>async</code> expression in C# 4.5. Compiler will rewrite such a function
into a state machine, thus function can suspend its execution, wait for the data and
to resume when data is available.
</p>
        <p>
Example:
</p>
        <p style="padding-left: 1em">
          <code>// read data asynchronously from an input and write it into an output.<br />
int cnt = 0;<br /><br />
do<br />
{<br />
cnt = await streamR.read(512, buf);<br /><br />
if (cnt == 0) 
<br />
{<br />
break;<br />
}<br /><br />
cnt = await streamW.write(cnt, buf);<br />
}<br />
while(cnt &gt; 0);</code>
        </p>
        <p>
It's iteresting to see how authors will address <code>yield return</code>: either
with aditional keyword, or in terms of resumable functions.
</p>
        <p>
        </p>
        <p>
          <table border="1">
            <tr>
              <td>
                <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3340.pdf"> N3340</a>
              </td>
              <td>
12-0030</td>
              <td>
Rich Pointers</td>
              <td>
D. M. Berris, M. Austern, L. Crowl</td>
            </tr>
          </table>
        </p>
        <p>
Here authors try to justify rich type-info but mask it under the name "rich pointers".
To make things even more obscure they argue about dynamic code generation.
</p>
        <p>
If you want a rich type-info then you should talk about it and not about thousand
of other things.
</p>
        <p>
We would better appealed to create a standard API to access post-compile object model,
which could be used to produce different type-infos or other source derivatives.
</p>
        <p>
This paper is our outsider. :-)
</p>
        <p>
        </p>
        <p>
          <table border="1">
            <tr>
              <td>
                <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3341.pdf"> N3341</a>
              </td>
              <td>
12-0031</td>
              <td>
Transactional Language Constructs for C++</td>
              <td>
M. Wong, H. Boehm, J. Gottschlich, T. Shpeisman, et al.</td>
            </tr>
          </table>
        </p>
        <p>
Here people try to generalize (put you away from) locking, and replace it with other
word "transaction". 
</p>
        <p>
Seems it's not viable proposition. It's better to teach on functional style
of programming with its immutable objects.
</p>
        <p>
        </p>
        <table border="1">
          <tr>
            <td>
              <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3347.pdf"> N3347</a>
            </td>
            <td>
12-0037</td>
            <td>
Modules in C++ (Revision 6)</td>
            <td>
Daveed Vandevoorde</td>
          </tr>
        </table>
        <p>
Author argues against C style source composition with <code>#include</code> directive,
and propose alternative called "modules".
</p>
        <p>
We think that many C++ developers would agree that C pre-processor is a legacy that
would never have existed, but for the same reason (for the legacy, and compatibility)
it should stay.
</p>
        <p>
In out opinion the current proposition is just immature, at least it's not intuitive.
Or in other words there should be something to replace the C pre-processor (and <code>#include</code> as
its part), but we don't like this paper from aestetic perspective.
</p>
        <p>
        </p>
        <table border="1">
          <tr>
            <td>
              <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3365.html"> N3365</a>
            </td>
            <td>
12-0055</td>
            <td>
Filesystem Library Proposal (Revision 2)</td>
          </tr>
        </table>
        <p>
This proposal says no a word about asynchronous nature of file access, while it should
be designed around it.
</p>
        <img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22" />
      </body>
      <title>New season in C++ WG</title>
      <guid isPermaLink="false">http://www.nesterovsky-bros.com/weblog/PermaLink,guid,a9c55d27-d353-4bcf-b923-2b7f00768d22.aspx</guid>
      <link>http://www.nesterovsky-bros.com/weblog/2012/03/16/NewSeasonInCWG.aspx</link>
      <pubDate>Fri, 16 Mar 2012 19:21:58 GMT</pubDate>
      <description>  &lt;p&gt;
After C++11 revision has been approved a new cycle of C++ design has begun:
&lt;/p&gt;
&lt;p style="padding-left: 1em; font-style: italic"&gt;
&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3370.html"&gt;N3370&lt;/a&gt;:
The C++ standards committee is soliciting proposals for additional library components.
Such proposals can range from small (addition of a single signature to an existing
library) to large (something bigger than any current standard library component). 
&lt;/p&gt;
&lt;p&gt;
At this stage it&amp;#39;s interesting to read papers, as authors try to express ideas
rather than to formulate sentences that should go into spec as it lately was.
&lt;/p&gt;
&lt;p&gt;
These are just several papers that we&amp;#39;ve found interesting:
&lt;/p&gt;
&lt;table border="1"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3322.pdf"&gt;N3322&lt;/a&gt; 
&lt;/td&gt;
&lt;td&gt;
12-0012 
&lt;/td&gt;
&lt;td&gt;
A Preliminary Proposal for a Static if 
&lt;/td&gt;
&lt;td&gt;
Walter E. Brown 
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3329.pdf"&gt;N3329&lt;/a&gt; 
&lt;/td&gt;
&lt;td&gt;
12-0019 
&lt;/td&gt;
&lt;td&gt;
Proposal: static if declaration 
&lt;/td&gt;
&lt;td&gt;
H. Sutter, W. Bright, A. Alexandrescu 
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;
Those proposals argue about compile time &amp;quot;if statement&amp;quot;. The feature can
replace &lt;code&gt;#if&lt;/code&gt; preprocessor directive, a SFINAE or in some cases template
specializations.
&lt;/p&gt;
&lt;p&gt;
A &lt;code&gt;static if&lt;/code&gt; declaration can appear wherever a declaration or a statement
is legal. Authors also propose to add &lt;code&gt;static if&lt;/code&gt; clause to a class and
a function declarations to conditionally exclude them from the scope.
&lt;/p&gt;
&lt;p&gt;
Examples:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;// Compile time factorial.&lt;br /&gt;
template &amp;lt;unsigned n&amp;gt;&lt;br /&gt;
struct factorial&lt;br /&gt;
{&lt;br /&gt;
static if (n &amp;lt;= 1)&lt;br /&gt;
{&lt;br /&gt;
enum : unsigned { value = 1 };&lt;br /&gt;
} 
&lt;br /&gt;
else 
&lt;br /&gt;
{&lt;br /&gt;
enum : unsigned { value = factorial&amp;lt;n - 1&amp;gt;::value * n };&lt;br /&gt;
}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
// Declare class provided a condition is true.&lt;br /&gt;
class Internals if (sizeof(void*) == sizeof(int));&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
Paper presents strong rationale why this addition helps to build better programs,
however the questions arise about relations between &lt;code&gt;static if&lt;/code&gt; and concepts, &lt;code&gt;static
if&lt;/code&gt; clause and an error diagnostics.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;table border="1"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3327.pdf"&gt; N3327&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;
12-0017&lt;/td&gt;
&lt;td&gt;
A Standard Programmatic Interface for Asynchronous Operations&lt;/td&gt;
&lt;td&gt;
N. Gustafsson, A. Laksberg&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3328.pdf"&gt; N3328&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;
12-0018&lt;/td&gt;
&lt;td&gt;
Resumable Functions&lt;/td&gt;
&lt;td&gt;
Niklas Gustafsson&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;
That&amp;#39;s our favorite.
&lt;/p&gt;
&lt;p&gt;
Authors propose an API and a language extensions to make asynchronous programs simpler. 
&lt;/p&gt;
&lt;p&gt;
In fact, asynchronous function will look very mush as a regular one but with small
additions. It&amp;#39;s similar to &lt;code&gt;yield return&lt;/code&gt; in C# (&lt;span style="font-style: italic"&gt;a
construct that has been available in C# for many years and is well vetted&lt;/span&gt;),
and to &lt;code&gt;async&lt;/code&gt; expression in C# 4.5. Compiler will rewrite such a function
into a state machine, thus function can suspend its execution, wait for the data and
to resume when data is available.
&lt;/p&gt;
&lt;p&gt;
Example:
&lt;/p&gt;
&lt;p style="padding-left: 1em"&gt;
&lt;code&gt;// read data asynchronously from an input and write it into an output.&lt;br /&gt;
int cnt = 0;&lt;br /&gt;
&lt;br /&gt;
do&lt;br /&gt;
{&lt;br /&gt;
cnt = await streamR.read(512, buf);&lt;br /&gt;
&lt;br /&gt;
if (cnt == 0) 
&lt;br /&gt;
{&lt;br /&gt;
break;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
cnt = await streamW.write(cnt, buf);&lt;br /&gt;
}&lt;br /&gt;
while(cnt &amp;gt; 0);&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
It&amp;#39;s iteresting to see how authors will address &lt;code&gt;yield return&lt;/code&gt;: either
with aditional keyword, or in terms of resumable functions.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;table border="1"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3340.pdf"&gt; N3340&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;
12-0030&lt;/td&gt;
&lt;td&gt;
Rich Pointers&lt;/td&gt;
&lt;td&gt;
D. M. Berris, M. Austern, L. Crowl&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;
Here authors try to justify rich type-info but mask it under the name &amp;quot;rich pointers&amp;quot;.
To make things even more obscure they argue about dynamic code generation.
&lt;/p&gt;
&lt;p&gt;
If you want a rich type-info then you should talk about it and not about thousand
of other things.
&lt;/p&gt;
&lt;p&gt;
We would better appealed to create a standard API to access post-compile object model,
which could be used to produce different type-infos or other source derivatives.
&lt;/p&gt;
&lt;p&gt;
This paper is our outsider. :&amp;#45;)
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;table border="1"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3341.pdf"&gt; N3341&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;
12-0031&lt;/td&gt;
&lt;td&gt;
Transactional Language Constructs for C++&lt;/td&gt;
&lt;td&gt;
M. Wong, H. Boehm, J. Gottschlich, T. Shpeisman, et al.&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;
Here people try to generalize (put you away from) locking, and replace it with other
word &amp;quot;transaction&amp;quot;. 
&lt;/p&gt;
&lt;p&gt;
Seems it&amp;#39;s not viable proposition. It&amp;#39;s better to teach on functional style
of programming with its immutable objects.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;table border="1"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3347.pdf"&gt; N3347&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;
12-0037&lt;/td&gt;
&lt;td&gt;
Modules in C++ (Revision 6)&lt;/td&gt;
&lt;td&gt;
Daveed Vandevoorde&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;
Author argues against C style source composition with &lt;code&gt;#include&lt;/code&gt; directive,
and propose alternative called &amp;quot;modules&amp;quot;.
&lt;/p&gt;
&lt;p&gt;
We think that many C++ developers would agree that C pre-processor is a legacy that
would never have existed, but for the same reason (for the legacy, and compatibility)
it should stay.
&lt;/p&gt;
&lt;p&gt;
In out opinion the current proposition is just immature, at least it&amp;#39;s not intuitive.
Or in other words there should be something to replace the C pre-processor (and &lt;code&gt;#include&lt;/code&gt; as
its part), but we don&amp;#39;t like this paper from aestetic perspective.
&lt;/p&gt;
&lt;p&gt;
&lt;/p&gt;
&lt;table border="1"&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3365.html"&gt; N3365&lt;/a&gt;&lt;/td&gt;
&lt;td&gt;
12-0055&lt;/td&gt;
&lt;td&gt;
Filesystem Library Proposal (Revision 2)&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;
This proposal says no a word about asynchronous nature of file access, while it should
be designed around it.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.nesterovsky-bros.com/weblog/aggbug.ashx?id=a9c55d27-d353-4bcf-b923-2b7f00768d22" /&gt;</description>
      <comments>http://www.nesterovsky-bros.com/weblog/CommentView,guid,a9c55d27-d353-4bcf-b923-2b7f00768d22.aspx</comments>
      <category>C++</category>
      <category>Thinking aloud</category>
    </item>
  </channel>
</rss>