<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss'><id>tag:blogger.com,1999:blog-16028823477632036</id><updated>2009-12-02T16:04:05.159-08:00</updated><title type='text'>C++ Cpp C Plus Plus</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default?start-index=26&amp;max-results=25'/><author><name>Way Greate</name><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>5000</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-2740728340001360623</id><published>2008-12-04T21:26:00.003-08:00</published><updated>2008-12-04T21:26:07.919-08:00</updated><title type='text'>Asking for more precision than is available = UB?</title><content type='html'>On Oct 16, 2:43 am, Thomas Richter &lt;t...@math.tu-berlin.de&gt; wrote:&lt;br /&gt;&gt; If you need more precision (you did check that you really need more&lt;br /&gt;&gt; precision, didn't you?) than what long double grants, you should check&lt;br /&gt;&gt; third-party libraries, for example the GNU bignum library:&lt;br /&gt;&lt;br /&gt;Thanks for your comments Thomas.  I'm aware of the various arbitrary-&lt;br /&gt;precision libraries which are available, but this question isn't&lt;br /&gt;really about needing extra precision for any practical purpose.  I'm&lt;br /&gt;just wondering if one of the 3 compilers is defective or if the code&lt;br /&gt;generates undefined behavior (including, say, a segfault) according to&lt;br /&gt;the standard.  I think it must be undefined behavior to ask for more&lt;br /&gt;precision than is available, I just wanted to check clc++m to find out&lt;br /&gt;for sure.&lt;br /&gt;&lt;br /&gt;--&lt;br /&gt;John&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-2740728340001360623?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/2740728340001360623/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=2740728340001360623' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/2740728340001360623'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/2740728340001360623'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/asking-for-more-precision-than-is_04.html' title='Asking for more precision than is available = UB?'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-6603963033562853921</id><published>2008-12-04T21:26:00.001-08:00</published><updated>2008-12-04T21:26:07.106-08:00</updated><title type='text'>gcc compiler warning: "will be initialized after/when initialized here"</title><content type='html'>Hi,&lt;br /&gt;&lt;br /&gt;Throughout my code, I use references to existing classes in other&lt;br /&gt;classes.  I declared these reference vars as&lt;br /&gt;&lt;br /&gt;class MyClass {&lt;br /&gt;    MyOther&amp; myother;&lt;br /&gt;&lt;br /&gt;    MyClass (MyOther&amp; other) : myother(other)&lt;br /&gt;    {&lt;br /&gt;        ...&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    ....&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;and initialize them in the constructor as shown above.  Why is GCC&lt;br /&gt;compiler giving me these warnings:&lt;br /&gt;&lt;br /&gt;will be initialized after&lt;br /&gt;when initialized here&lt;br /&gt;&lt;br /&gt;(Intel compiler didn't complain, but I want to use GCC now for Linux.)&lt;br /&gt;&lt;br /&gt;Thanks,&lt;br /&gt;Andy&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-6603963033562853921?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/6603963033562853921/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=6603963033562853921' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/6603963033562853921'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/6603963033562853921'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/gcc-compiler-warning-be-initialized.html' title='gcc compiler warning: &amp;quot;will be initialized after/when initialized here&amp;quot;'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-6262203836677102433</id><published>2008-12-04T20:26:00.003-08:00</published><updated>2008-12-04T20:26:14.496-08:00</updated><title type='text'>Asking for more precision than is available = UB?</title><content type='html'>John W. Peterson ha scritto:&lt;br /&gt;&gt; I'm pretty sure the following code causes undefined behavior (since I&lt;br /&gt;&gt; scanned through my draft of the standard and didn't find anything) but&lt;br /&gt;&gt; I just wanted to be sure.  Essentially I am asking for more precision&lt;br /&gt;&gt; to be printed out than is available.  Two compilers (Intel 10 and GCC&lt;br /&gt;&gt; 4.0.1) print about 20 correct digits and the rest are garbage, the PGI&lt;br /&gt;&gt; C++ compiler won't recognize (AFAICT) any precision setting beyond 16&lt;br /&gt;&gt; (or maybe it's all zeros and they aren't printed?)  The test code and&lt;br /&gt;&gt; some results follow below, thanks in advance for your input.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I don't think it's a case of undefined behaviour, but it's simply&lt;br /&gt;implementation-dependent behaviour.&lt;br /&gt;&lt;br /&gt;The "garbage" after the first 20 correct digits is, technically&lt;br /&gt;speaking, no garbage at all. It's truly the value of ld, that stores the&lt;br /&gt;value of the literal as accurately as possible. As the value of the&lt;br /&gt;literal lies between two representable floating point values, the&lt;br /&gt;implementation simply chooses one of the two. The loss of accuracy&lt;br /&gt;therefore happens in the first line (the assignment) rather than in the&lt;br /&gt;third line (the output).&lt;br /&gt;&lt;br /&gt;By the way, I believe PGI has a bug, because according to my&lt;br /&gt;interpretation the output shall contain the exact specified number of&lt;br /&gt;digits after the ".", the implementation is not allowed to output less&lt;br /&gt;digits.&lt;br /&gt;&lt;br /&gt;HTH,&lt;br /&gt;&lt;br /&gt;Ganesh&lt;br /&gt;&lt;br /&gt;&gt; #include &lt;iostream&gt;&lt;br /&gt;&gt; int main()&lt;br /&gt;&gt; {&lt;br /&gt;&gt;    long double ld=3.1415926535897932384626433832795029L;&lt;br /&gt;&gt;    std::cout.precision(32);&lt;br /&gt;&gt;    std::cout &lt;&lt; "ld=" &lt;&lt; ld &lt;&lt; std::endl;&lt;br /&gt;&gt;    return 0;&lt;br /&gt;&gt; }&lt;br /&gt;&gt; &lt;br /&gt;&gt; &lt;br /&gt;&gt; Intel C++ prints:&lt;br /&gt;&gt; ld=3.1415926535897932385128089594062&lt;br /&gt;&gt; &lt;br /&gt;&gt; PGI prints:&lt;br /&gt;&gt; ld=3.1415926535897931&lt;br /&gt;&gt; &lt;br /&gt;&gt; GCC prints:&lt;br /&gt;&gt; ld=3.1415926535897932385128089594062&lt;br /&gt;&gt; &lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-6262203836677102433?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/6262203836677102433/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=6262203836677102433' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/6262203836677102433'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/6262203836677102433'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/asking-for-more-precision-than-is.html' title='Asking for more precision than is available = UB?'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-9124571956614633918</id><published>2008-12-04T20:26:00.001-08:00</published><updated>2008-12-04T20:26:11.742-08:00</updated><title type='text'>Are throwing default constructors bad style, and if so, why?</title><content type='html'>JoshuaMaurice@gmail.com wrote:&lt;br /&gt;&gt; On Oct 15, 3:48 pm, "Nevin :-] Liber" &lt;ne...@eviloverlord.com&gt; wrote:&lt;br /&gt;&gt;&gt; The decision not to use exceptions pretty much has to be done from the&lt;br /&gt;&gt;&gt; ground up, and it will result in a code base that is harder to get&lt;br /&gt;&gt;&gt; correct, harder to maintain, and overall might end up slower.&lt;br /&gt;&gt; &lt;br /&gt;&gt; I agree completely. Exception use is so pervasive in normal C++ that&lt;br /&gt;&gt; it's quite difficult to isolate and squash exceptions.&lt;br /&gt;&gt; &lt;br /&gt;&gt; However, my company is trying: No exceptions anywhere. The performance&lt;br /&gt;&gt; critical sections are a like a flashback of bad C I would expect to&lt;br /&gt;&gt; see 15 years ago.&lt;br /&gt;&gt; &lt;br /&gt;&gt; I was going to attempt to persuade them that exceptions are good, but&lt;br /&gt;&gt; the argument is a little harder now in light of the my somewhat-&lt;br /&gt;&gt; trivial tests showing significant exception handling overhead when no&lt;br /&gt;&gt; exception is thrown.&lt;br /&gt;&lt;br /&gt;Yes but that is not the consequence of using exceptions but of using a &lt;br /&gt;bad compiler. What your company is doing is writing hard to maintain &lt;br /&gt;poor quality C++ because you have determined that a specific compiler is &lt;br /&gt;  making the presence throwing an exception expensive even if it is &lt;br /&gt;never thrown.&lt;br /&gt;&lt;br /&gt;We now know that exceptions can be implemented on normal platforms with &lt;br /&gt;close to zero overhead (less than the cost of using error returns). &lt;br /&gt;There can be a case for writing exception free programs (note that, no &lt;br /&gt;exceptions anywhere) for some limited resource (usually embedded) platforms.&lt;br /&gt;&lt;br /&gt;Doing that involves avoiding a great deal of library resources and &lt;br /&gt;should therefore not be undertaken lightly.&lt;br /&gt;&lt;br /&gt;As a general engineering principle (not specific to software) one avoids &lt;br /&gt;tools that make the product more expensive in development, construction &lt;br /&gt;and maintenance.&lt;br /&gt;&lt;br /&gt;What your evidence should do is to produce a very strong case for your &lt;br /&gt;employers to switch to other compilers that do not cause them to have to &lt;br /&gt;choose between seriously degraded performance and excessively high &lt;br /&gt;development and maintenance costs.&lt;br /&gt;&lt;br /&gt;&gt; &lt;br /&gt;&gt; Funnily enough, I'm pretty sure we don't actually catch all exceptions&lt;br /&gt;&gt; from new and the standard library, meaning we get the worst of both&lt;br /&gt;&gt; worlds: bad code from exception overhead paranoia and slow code from&lt;br /&gt;&gt; the exception overhead we didn't deal with.&lt;br /&gt;&gt; &lt;br /&gt;&gt; I'm of the opinion that if it really is a problem, a profiler will&lt;br /&gt;&gt; pick it up, and we could take time to squash exceptions in that&lt;br /&gt;&gt; performance critical area.&lt;br /&gt;&gt; &lt;br /&gt;&lt;br /&gt;As always one should focus on optimising code where it is proved that it &lt;br /&gt;matters. One should always endeavour to contain such code so that when &lt;br /&gt;the problem goes away it can be rewritten in a clean and easily &lt;br /&gt;maintainable form.&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-9124571956614633918?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/9124571956614633918/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=9124571956614633918' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/9124571956614633918'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/9124571956614633918'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/are-throwing-default-constructors-bad_9421.html' title='Are throwing default constructors bad style, and if so, why?'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-4043306371767942244</id><published>2008-12-04T19:26:00.003-08:00</published><updated>2008-12-04T19:26:27.196-08:00</updated><title type='text'>vector&lt;&gt;.erase(where) vs vector&lt;&gt;.erase(from, to)</title><content type='html'>eric ha scritto:&lt;br /&gt;&gt; &lt;br /&gt;&gt; This means that the remove-erase idiom can't be used in this case:&lt;br /&gt;&gt;     v.erase(remove(v.begin(), v.end(), 0));&lt;br /&gt;&gt; will give undefined behaviour.&lt;br /&gt;&gt; &lt;br /&gt;&lt;br /&gt;Even in cases where it doesn't invoke UB (that is, if you have at least&lt;br /&gt;one 0 in the vector) the code wouldn't behave correctly in the case you&lt;br /&gt;have more that one 0. In that case, erase() will remove only one element&lt;br /&gt;and you end up with n-1 garbage elements at the end of the vector (where&lt;br /&gt;n is the number of 0s).&lt;br /&gt;&lt;br /&gt;Morale: don't do it.&lt;br /&gt;&lt;br /&gt;If you know in advance that there is *exactly* one 0, it's more&lt;br /&gt;efficient to use:&lt;br /&gt;&lt;br /&gt;  v.erase(find(v.begin(), v.end(), 0));&lt;br /&gt;&lt;br /&gt;in all other cases, use the "correct" remove idiom:&lt;br /&gt;&lt;br /&gt;  v.erase(remove(v.begin(), v.end(), 0), v.end());&lt;br /&gt;&lt;br /&gt;HTH,&lt;br /&gt;&lt;br /&gt;Ganesh&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-4043306371767942244?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/4043306371767942244/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=4043306371767942244' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/4043306371767942244'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/4043306371767942244'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/vector-vs-vector-to_2824.html' title='vector&amp;lt;&amp;gt;.erase(where) vs vector&amp;lt;&amp;gt;.erase(from, to)'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-4285312295353436607</id><published>2008-12-04T19:26:00.001-08:00</published><updated>2008-12-04T19:26:25.872-08:00</updated><title type='text'>c++0x: static_assert &amp; concepts</title><content type='html'>SG ha scritto:&lt;br /&gt;&gt; This is a quote from Kanny Kalev's article "Static Assertions":&lt;br /&gt;&gt; ( http://www.informit.com/guides/content.aspx?g=cplusplus&amp;seqNum=343&amp;rll=1&lt;br /&gt;&gt; )&lt;br /&gt;&gt; &lt;br /&gt;&gt; "The concepts proposal doesnât eliminate the need for static&lt;br /&gt;&gt; assertions in C++. [...] static assertions are useful in C++ programs&lt;br /&gt;&gt; that do not use templates at all. [...]&lt;br /&gt;&lt;br /&gt;So far, so good.&lt;br /&gt;&lt;br /&gt;&gt; [...] static assertions, when used&lt;br /&gt;&gt; together with the TR1 type_traits library, can diagnose type errors&lt;br /&gt;&gt; that intrinsic support for concepts cannot, such as ensuring that the&lt;br /&gt;&gt; template argument of basic_string shall be a POD type."&lt;br /&gt;&lt;br /&gt;This I disagree. I don't see why concepts should be unable to ensure&lt;br /&gt;POD-ness of the character type. Ok, we don't have a std::Pod concept&lt;br /&gt;right now in the C++0x working paper, but we could have it, if it's&lt;br /&gt;needed. Consider that even std::is_pod needs some compiler magic to be&lt;br /&gt;implemented properly; a similar magic could be used to provide implicit&lt;br /&gt;concept maps for an hypothetical std::Pod concept as it's already&lt;br /&gt;required for std::StandardLayout and others.&lt;br /&gt;&lt;br /&gt;&gt; This sounds like we still need traits classes. It's unfortunate that&lt;br /&gt;&gt; the "boolean value" of a concept-check isn't easily acessible for meta&lt;br /&gt;&gt; programming and static_assert. There is a way to turn an old-style&lt;br /&gt;&gt; compile-time predicate (template&lt;..&gt; struct {..};) into a concept via&lt;br /&gt;&gt; std::True. But it doesn't work the other way around, does it?&lt;br /&gt;&lt;br /&gt;Yes, traits classes still have their use in the concept world.&lt;br /&gt;&lt;br /&gt;We don't have a way as compact as std::True to do it, but turning a&lt;br /&gt;concept into a trait class is not that difficult:&lt;br /&gt;&lt;br /&gt;  concept MyConcept { /* ... */ }&lt;br /&gt;&lt;br /&gt;  template &lt;typename T&gt;&lt;br /&gt;  struct is_my_concept : std::false_type {};&lt;br /&gt;&lt;br /&gt;  template &lt;MyConcept T&gt;&lt;br /&gt;  struct is_my_concept&lt;T&gt; : std::true_type {};&lt;br /&gt;&lt;br /&gt;HTH,&lt;br /&gt;&lt;br /&gt;Ganesh&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-4285312295353436607?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/4285312295353436607/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=4285312295353436607' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/4285312295353436607'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/4285312295353436607'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/c0x-staticassert-concepts_04.html' title='c++0x: static_assert &amp;amp; concepts'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-8565839769729692286</id><published>2008-12-04T18:26:00.003-08:00</published><updated>2008-12-04T18:26:16.005-08:00</updated><title type='text'>c++0x: static_assert &amp; concepts</title><content type='html'>On 16 oct, 00:38, SG &lt;s.gesem...@gmail.com&gt; wrote:&lt;br /&gt;&gt; There is a way to turn an old-style&lt;br /&gt;&gt; compile-time predicate (template&lt;..&gt; struct {..};) into a concept via&lt;br /&gt;&gt; std::True. But it doesn't work the other way around, does it?&lt;br /&gt;&lt;br /&gt;template&lt;typename T&gt;&lt;br /&gt;struct is_foo&lt;br /&gt;{&lt;br /&gt;    static const bool value = false;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;template&lt;Foo T&gt;&lt;br /&gt;struct is_foo&lt;T&gt;&lt;br /&gt;{&lt;br /&gt;    static const bool value = true;&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;I didn't know it was possible to turn a meta-function into a concept.&lt;br /&gt;How is it done?&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-8565839769729692286?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/8565839769729692286/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=8565839769729692286' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/8565839769729692286'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/8565839769729692286'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/c0x-staticassert-concepts.html' title='c++0x: static_assert &amp;amp; concepts'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-7187130012810001381</id><published>2008-12-04T18:26:00.001-08:00</published><updated>2008-12-04T18:26:14.979-08:00</updated><title type='text'>vector&lt;&gt;.erase(where) vs vector&lt;&gt;.erase(from, to)</title><content type='html'>JoshuaMaurice@gmail.com wrote:&lt;br /&gt;&gt; If you could post a complete code sample, I might be better able to&lt;br /&gt;&gt; help.&lt;br /&gt;OK, here goes:&lt;br /&gt;&lt;br /&gt;#include &lt;iostream&gt;&lt;br /&gt;#include &lt;vector&gt;&lt;br /&gt;#include &lt;algorithm&gt;&lt;br /&gt;using std::cout;&lt;br /&gt;using std::vector;&lt;br /&gt;using std::remove;&lt;br /&gt;&lt;br /&gt;template &lt;class T&gt;&lt;br /&gt;void print(const T&amp; t)&lt;br /&gt;{&lt;br /&gt;     for (typename T::const_iterator it = t.begin(); it != t.end(); ++it)&lt;br /&gt;         cout &lt;&lt; *it &lt;&lt; ' ';&lt;br /&gt;     cout &lt;&lt; '\n';&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main(int, char**)&lt;br /&gt;{&lt;br /&gt;     int k[] = { 1, 2, 3, 4, 5, 6, 7, 8 };&lt;br /&gt;     vector&lt;int&gt; v(k, k + 8);&lt;br /&gt;	&lt;br /&gt;     print(v);&lt;br /&gt;	&lt;br /&gt;     int to_be_found = 0;&lt;br /&gt;     vector&lt;int&gt;::iterator it = remove(v.begin(), v.end(), to_be_found);&lt;br /&gt;	&lt;br /&gt;     if (it == v.end())&lt;br /&gt;         cout &lt;&lt; to_be_found &lt;&lt; " not found\n";&lt;br /&gt;		&lt;br /&gt;     // Single iterator version of erase cannot be used if iterator&lt;br /&gt;     //points to end, becaue the Windows version erases the last element&lt;br /&gt;     //and the Linux version crashes&lt;br /&gt;     v.erase(it);        // This exhibits undefined (?) behaviour&lt;br /&gt;     //v.erase(it, it);  // This always works&lt;br /&gt;     print(v);&lt;br /&gt;     return 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;     output using VC++&lt;br /&gt;&lt;br /&gt;     1 2 3 4 5 6 7 8&lt;br /&gt;     not found&lt;br /&gt;     1 2 3 4 5 6 7&lt;br /&gt;&lt;br /&gt;     output using g++&lt;br /&gt;&lt;br /&gt;     1 2 3 4 5 6 7 8&lt;br /&gt;     not found&lt;br /&gt;     Segmentation fault&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-7187130012810001381?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/7187130012810001381/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=7187130012810001381' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/7187130012810001381'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/7187130012810001381'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/vector-vs-vector-to_04.html' title='vector&amp;lt;&amp;gt;.erase(where) vs vector&amp;lt;&amp;gt;.erase(from, to)'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-2602653648869781487</id><published>2008-12-04T17:26:00.003-08:00</published><updated>2008-12-04T17:26:31.598-08:00</updated><title type='text'>Can COM class be used as Template Class</title><content type='html'>On Oct 15, 6:37 pm, Piaoger Gong &lt;piao...@gmail.com&gt; wrote:&lt;br /&gt;&gt; Hi,&lt;br /&gt;&gt;&lt;br /&gt;&gt; template&lt;class T&gt;&lt;br /&gt;&gt; class CMyHandler&lt;br /&gt;&gt; {&lt;br /&gt;&gt; public:&lt;br /&gt;&gt;    void DoSomething(int num, CComPtr&lt;T&gt; spiNode)&lt;br /&gt;&gt;   {&lt;br /&gt;&gt;     spiNode-&gt;Remove();&lt;br /&gt;&gt;    }&lt;br /&gt;&gt;&lt;br /&gt;&gt; };&lt;br /&gt;&gt;&lt;br /&gt;&gt; int main()&lt;br /&gt;&gt; {&lt;br /&gt;&gt;    CMyHandler&lt;COMClass&gt; handler;&lt;br /&gt;&gt;    handler.DoSomething();&lt;br /&gt;&gt;    return 1;&lt;br /&gt;&gt;&lt;br /&gt;&gt; }&lt;br /&gt;&gt;&lt;br /&gt;&gt; The above code will get a link error for ComClass::DoSomething is&lt;br /&gt;&gt; unresolved.&lt;br /&gt;&lt;br /&gt;No, ComClass::DoSomething is only part of that function's signature.&lt;br /&gt;&lt;br /&gt;&gt; if I have ComClass::DoSomething called outside of template class. No&lt;br /&gt;&gt; link error again.&lt;br /&gt;&gt;&lt;br /&gt;&gt; Is there any method for this issue and I am thirsty for you solution.&lt;br /&gt;&gt;&lt;br /&gt;&gt; Thanks invance,&lt;br /&gt;&gt; Piaoger&lt;br /&gt;&gt;&lt;br /&gt;&lt;br /&gt;A function has a signature.&lt;br /&gt;Same goes for non-static member functions&lt;br /&gt;(which additionally includes a this parameter).&lt;br /&gt;&lt;br /&gt;void DoSomething( int n ) { } // 1&lt;br /&gt;&lt;br /&gt;and&lt;br /&gt;&lt;br /&gt;void DoSomething() { }&lt;br /&gt;&lt;br /&gt;are two completely different functions.&lt;br /&gt;So if you declare and implement 1 and then try to call DoSomething()&lt;br /&gt;it will be unresolved, as expected. The only part of a function&lt;br /&gt;declaration not considered to be part of its signature is its return&lt;br /&gt;type (if any).&lt;br /&gt;&lt;br /&gt;If you were to provide a default value for a given arguement:&lt;br /&gt;&lt;br /&gt;void DoSomething( const int n = 5 ) { }&lt;br /&gt;&lt;br /&gt;Then you could call DoSomething() since that is really DoSomething(5).&lt;br /&gt;We have a signature match for the function call. DoSomething( const&lt;br /&gt;int ).&lt;br /&gt;There are additional rules in the case you choose to provide default&lt;br /&gt;values to some, not all, parameters. Lookup those details in your fav C&lt;br /&gt;++ book.&lt;br /&gt;&lt;br /&gt;Not sure if thats what you are looking for.&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-2602653648869781487?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/2602653648869781487/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=2602653648869781487' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/2602653648869781487'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/2602653648869781487'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/can-com-class-be-used-as-template-class_7235.html' title='Can COM class be used as Template Class'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-9092868941593471192</id><published>2008-12-04T17:26:00.001-08:00</published><updated>2008-12-04T17:26:30.597-08:00</updated><title type='text'>Can COM class be used as Template Class</title><content type='html'>Sorry. The subject should be "Can COM class be used as Template&lt;br /&gt;argument".&lt;br /&gt;If someone can provide some information about why and how to resolve&lt;br /&gt;the link error when we want to call COM class method, It will be much&lt;br /&gt;appreciated.&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-9092868941593471192?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/9092868941593471192/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=9092868941593471192' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/9092868941593471192'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/9092868941593471192'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/can-com-class-be-used-as-template-class_04.html' title='Can COM class be used as Template Class'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-8152563223751710731</id><published>2008-12-04T14:26:00.003-08:00</published><updated>2008-12-04T14:26:26.699-08:00</updated><title type='text'>vector&lt;&gt;.erase(where) vs vector&lt;&gt;.erase(from, to)</title><content type='html'>eric &lt;eric@bakkerspees.nl&gt; wrote in&lt;br /&gt;news:48f5f7bc$0$5038$e4fe514c@dreader26.news.xs4all.nl:&lt;br /&gt;&lt;br /&gt;&gt; Hi all,&lt;br /&gt;&gt; &lt;br /&gt;&gt; Suppose we have a vector of integers:&lt;br /&gt;&gt;      vector&lt;int&gt; v;&lt;br /&gt;&gt; filled with some values but not the integer 0 (zero).&lt;br /&gt;&gt; &lt;br /&gt;&gt; Next, we try to remove the zero from the vector:&lt;br /&gt;&gt;      vector&lt;int&gt;::iterator it = remove(v.begin(), v.end(), 0);&lt;br /&gt;&gt; Of course, the returned iterator equals v.end().&lt;br /&gt;&gt; &lt;br /&gt;&gt; Now comes the rub: when we erase the element using the iterator&lt;br /&gt;&gt; range version&lt;br /&gt;&gt;      v.erase(it, v.end());&lt;br /&gt;&gt; all is fine.&lt;br /&gt;&gt; &lt;br /&gt;&gt; But when we use the single iterator version (for example,&lt;br /&gt;&gt; because I already know that there is only one zero in the&lt;br /&gt;&gt; vector): &lt;br /&gt;&gt;      v.erase(it);&lt;br /&gt;&gt; Bad Things Happen: Windows/Visual C++ removes the last element&lt;br /&gt;&gt; of the vector and Linux/g++ segfaults.&lt;br /&gt;&lt;br /&gt;You are contradicting yourself. First you say that the vector does&lt;br /&gt;not contain any zeros, then you say that you "already know that&lt;br /&gt;there is only one zero". Both can't be true at the same time.&lt;br /&gt;&lt;br /&gt;&gt; &lt;br /&gt;&gt; This means that the remove-erase idiom can't be used in this&lt;br /&gt;&gt; case: &lt;br /&gt;&gt;      v.erase(remove(v.begin(), v.end(), 0));&lt;br /&gt;&gt; will give undefined behaviour.&lt;br /&gt;&lt;br /&gt;The erase-remove idiom uses the iterator range version of erase:&lt;br /&gt;&lt;br /&gt;     	v.erase( remove( v.begin(), v.end(), 0 ), v.end() );&lt;br /&gt;&lt;br /&gt;MV&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;I do not want replies; please follow-up to the group.&lt;br /&gt;&lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-8152563223751710731?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/8152563223751710731/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=8152563223751710731' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/8152563223751710731'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/8152563223751710731'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/vector-vs-vector-to.html' title='vector&amp;lt;&amp;gt;.erase(where) vs vector&amp;lt;&amp;gt;.erase(from, to)'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-1387461724008571177</id><published>2008-12-04T14:26:00.001-08:00</published><updated>2008-12-04T14:26:23.342-08:00</updated><title type='text'>question regarding std::9.4.2/4 (static data members of const integral
type)</title><content type='html'>Hi,&lt;br /&gt;&lt;br /&gt;when looking into certain headers of the Boost library, I found that&lt;br /&gt;the case of&lt;br /&gt;static const integral members of class templates is treated&lt;br /&gt;differently from case to case.&lt;br /&gt;For example (after expanding macros):&lt;br /&gt;&lt;br /&gt;boost/type_traits/detail/ice_eq.hpp:&lt;br /&gt;&lt;br /&gt;    template &lt;int b1, int b2&gt;&lt;br /&gt;    struct ice_eq&lt;br /&gt;    {&lt;br /&gt;        static const bool value = (b1 == b2);&lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;    template &lt;int b1, int b2&gt;&lt;br /&gt;    struct ice_ne&lt;br /&gt;    {&lt;br /&gt;        static const bool value = (b1 != b2);&lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;    template &lt;int b1, int b2&gt; bool const ice_eq&lt;b1,b2&gt;::value;&lt;br /&gt;    template &lt;int b1, int b2&gt; bool const ice_ne&lt;b1,b2&gt;::value;&lt;br /&gt;&lt;br /&gt;But on the other hand:&lt;br /&gt;&lt;br /&gt;boost/type_traits/detail/ice_or.hpp:&lt;br /&gt;&lt;br /&gt;    template &lt;bool b1, bool b2, bool b3 = false, bool b4 = false,&lt;br /&gt;                       bool b5 = false, bool b6 = false, bool b7 =&lt;br /&gt;false&gt;&lt;br /&gt;    struct ice_or;&lt;br /&gt;&lt;br /&gt;    template &lt;bool b1, bool b2, bool b3, bool b4, bool b5, bool b6,&lt;br /&gt;bool b7&gt;&lt;br /&gt;    struct ice_or&lt;br /&gt;    {&lt;br /&gt;        static const bool value = true;&lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;    template &lt;&gt;&lt;br /&gt;    struct ice_or&lt;false, false, false, false, false, false, false&gt;&lt;br /&gt;    {&lt;br /&gt;        static const bool value = false;&lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;    // No namespace scope definitions of ice_or&lt;...&gt;::value&lt;br /&gt;&lt;br /&gt;boost/type_traits/detail/cv_traits_impl.hpp:&lt;br /&gt;&lt;br /&gt;    template &lt;typename T&gt; struct cv_traits_imp {};&lt;br /&gt;&lt;br /&gt;    template &lt;typename T&gt;&lt;br /&gt;    struct cv_traits_imp&lt;T*&gt;&lt;br /&gt;    {&lt;br /&gt;        static const bool is_const = false;&lt;br /&gt;        static const bool is_volatile = false;&lt;br /&gt;        typedef T unqualified_type;&lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;    ....&lt;br /&gt;&lt;br /&gt;    // No namespace scope definitions of cv_traits_imp&lt;...&gt;::is_const/&lt;br /&gt;is_volatile&lt;br /&gt;&lt;br /&gt;At least ice_or&lt;...&gt;::value is used (e.g. in the definition of&lt;br /&gt;is_base_of).&lt;br /&gt;&lt;br /&gt;Is the Boost code incorrect or do I read paragraph 9.4.2/4 in a wrong&lt;br /&gt;way?&lt;br /&gt;Is this paragraph applicable for class template instantiations? (I&lt;br /&gt;don't remember&lt;br /&gt;a paragraph, which states that an instantiation of a class template is&lt;br /&gt;a class)?&lt;br /&gt;&lt;br /&gt;Thanks for clarification,&lt;br /&gt;Norbert&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-1387461724008571177?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/1387461724008571177/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=1387461724008571177' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/1387461724008571177'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/1387461724008571177'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/question-regarding-std9424-static-data.html' title='question regarding std::9.4.2/4 (static data members of const integral
type)'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-3824532613155518815</id><published>2008-12-04T13:27:00.003-08:00</published><updated>2008-12-04T13:27:12.456-08:00</updated><title type='text'>Can COM class be used as Template Class</title><content type='html'>Hi,&lt;br /&gt;&lt;br /&gt;template&lt;class T&gt;&lt;br /&gt;class CMyHandler&lt;br /&gt;{&lt;br /&gt;public:&lt;br /&gt;   void DoSomething(int num, CComPtr&lt;T&gt; spiNode)&lt;br /&gt;  {&lt;br /&gt;    spiNode-&gt;Remove();&lt;br /&gt;   }&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;   CMyHandler&lt;COMClass&gt; handler;&lt;br /&gt;   handler.DoSomething();&lt;br /&gt;   return 1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above code will get a link error for ComClass::DoSomething is&lt;br /&gt;unresolved.&lt;br /&gt;if I have ComClass::DoSomething called outside of template class. No&lt;br /&gt;link error again.&lt;br /&gt;&lt;br /&gt;Is there any method for this issue and I am thirsty for you solution.&lt;br /&gt;&lt;br /&gt;Thanks invance,&lt;br /&gt;Piaoger&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-3824532613155518815?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/3824532613155518815/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=3824532613155518815' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/3824532613155518815'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/3824532613155518815'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/can-com-class-be-used-as-template-class.html' title='Can COM class be used as Template Class'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-4737275354049212591</id><published>2008-12-04T13:27:00.001-08:00</published><updated>2008-12-04T13:27:11.413-08:00</updated><title type='text'>stringstream misunderstanding</title><content type='html'>Hi,&lt;br /&gt;&lt;br /&gt;The reasoning behind the interface is that stringstream is not&lt;br /&gt;implemented in terms of a string, but to format things passed to the&lt;br /&gt;stream as a string.&lt;br /&gt;&lt;br /&gt;The representation inside a stringstream (inside the string buffer&lt;br /&gt;actually) is optimized for speed and memory usage (which is why it's&lt;br /&gt;better to use stringstream instead of string for more complex string&lt;br /&gt;operations).&lt;br /&gt;&lt;br /&gt;The str() function doesn't return the internal representation of the&lt;br /&gt;string, as in the internal representation there is no string. Instead,&lt;br /&gt;it builds a string upon request and returns that. The string is a&lt;br /&gt;copy, not the internal representation.&lt;br /&gt;&lt;br /&gt;This is (should be) lazy evaluation, as the string is constructed only&lt;br /&gt;if needed (and returned as a temporary).&lt;br /&gt;&lt;br /&gt;The same holds true for the str(std::string const&amp;) function: it&lt;br /&gt;adjusts the internal buffer of the stringstream to copy the contents&lt;br /&gt;of the string. In broad terms (and broad terms only) it copies the&lt;br /&gt;string internally.&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-4737275354049212591?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/4737275354049212591/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=4737275354049212591' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/4737275354049212591'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/4737275354049212591'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/stringstream-misunderstanding_04.html' title='stringstream misunderstanding'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-4527663348117295124</id><published>2008-12-04T12:25:00.003-08:00</published><updated>2008-12-04T12:25:47.910-08:00</updated><title type='text'>Why the decision to leave typeinfo::name() out of the standard?</title><content type='html'>Sebastian Karlsson wrote:&lt;br /&gt;&gt; I understand that there's some grey areas, hence the need for some&lt;br /&gt;&gt; standard. I can see why it would be advantageous for memory&lt;br /&gt;&gt; constrained platforms to return an empty string here, and perhaps that&lt;br /&gt;&gt; could be fine. The problem for me is that I need a unique ID which I&lt;br /&gt;&gt; can serialize to be able to later on de serialize from a base pointer,&lt;br /&gt;&gt; it's the same problem which boost::serialize have pretty much. The&lt;br /&gt;&gt; gains from a standardized name() would mainly be for data sharing&lt;br /&gt;&gt; between the same application compiled for different platforms.&lt;br /&gt;&lt;br /&gt;The usual solution is for the base type to have a method returning an&lt;br /&gt;integral identifier of its derived type.  The deserializer loads the&lt;br /&gt;base object, gets the type ID, and static_casts to the derived type.&lt;br /&gt;&lt;br /&gt;Look Ma, no strings!&lt;br /&gt;&lt;br /&gt;if (base* const b = loadNextObject()) {&lt;br /&gt;&lt;br /&gt;     type_id const id = b-&gt;type_id();&lt;br /&gt;&lt;br /&gt;     switch (id) {&lt;br /&gt;       case d1_type_id:&lt;br /&gt;         work_with_d1(static_cast&lt;d1*&gt;(b));&lt;br /&gt;         break;&lt;br /&gt;       case d2_type_id:&lt;br /&gt;         work_with_d2(static_cast&lt;d2*&gt;(b));&lt;br /&gt;         break;&lt;br /&gt;       default:&lt;br /&gt;         throw exception("unknown type in input stream");&lt;br /&gt;     }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-4527663348117295124?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/4527663348117295124/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=4527663348117295124' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/4527663348117295124'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/4527663348117295124'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/why-decision-to-leave-typeinfoname-out_5680.html' title='Why the decision to leave typeinfo::name() out of the standard?'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-4593884468200711260</id><published>2008-12-04T12:25:00.001-08:00</published><updated>2008-12-04T12:25:46.585-08:00</updated><title type='text'>Questions about C++0x standard</title><content type='html'>Brendan ha scritto:&lt;br /&gt;&gt; I have a couple of questions about the forthcoming c++0x standard.&lt;br /&gt;&gt; &lt;br /&gt;&gt; Why are constexpr's not allowed to be recursive (according to the&lt;br /&gt;&gt; wikipedia article)? We are still allowed to do things like this&lt;br /&gt;&gt; correct?&lt;br /&gt;&gt; &lt;br /&gt;&gt; template &lt;typename T&gt;&lt;br /&gt;&gt; constexpr sum(T x) {&lt;br /&gt;&gt; return x;&lt;br /&gt;&gt; }&lt;br /&gt;&gt; &lt;br /&gt;&gt; template &lt;typename T, typenameâ¦ Args)&lt;br /&gt;&gt; constexpr sum(T x, Argsâ¦ args) {&lt;br /&gt;&gt; return x + sum(args);&lt;br /&gt;&gt; }&lt;br /&gt;&gt; &lt;br /&gt;&gt; because that would be enourmously useful, and much cleaner than the&lt;br /&gt;&gt; sum&lt;1,2,3&gt;::val syntax that template metaprogramming requires today.&lt;br /&gt;&gt; In general, I imagine a reduce implementation along these lines that&lt;br /&gt;&gt; could be used at both compile and runtime.&lt;br /&gt;&lt;br /&gt;AFAIK, the current working paper doesn't allow that. The thing is still&lt;br /&gt;under discussion and might change before the standard is approved,&lt;br /&gt;although chances are few.&lt;br /&gt;&lt;br /&gt;&gt; &lt;br /&gt;&gt; Also, in the new variadic templates, are variadic template template&lt;br /&gt;&gt; arguments permissiable? i.e. can I do stuff like this?&lt;br /&gt;&gt; &lt;br /&gt;&gt; // typelist element.&lt;br /&gt;&gt; template &lt;class Klass,&lt;br /&gt;&gt;            typename RetType,&lt;br /&gt;&gt;            template &lt;typename... Args&gt;&lt;br /&gt;&gt;            RetType (Klass::*Ptr)(Args)&gt;&lt;br /&gt;&gt; struct mem_fun_spec {&lt;br /&gt;&gt;    typedef Klass klass;&lt;br /&gt;&gt;    typedef RetType ret_type;&lt;br /&gt;&gt;    typedef Ptr ptr;&lt;br /&gt;&gt; };&lt;br /&gt;&lt;br /&gt;You will be able to have template template arguments with variable&lt;br /&gt;number of arguments, but you can't write that code because:&lt;br /&gt;&lt;br /&gt;   template &lt;typename... Args&gt; RetType (Klass::*Ptr)(Args)&lt;br /&gt;&lt;br /&gt;is not a template *template* argument (if it were legal it would be a&lt;br /&gt;template *non-type* argument).&lt;br /&gt;&lt;br /&gt;&gt; Finally... on a similar topic. Are we ever going to get some way of&lt;br /&gt;&gt; getting a typelist of a complete types members? Something like&lt;br /&gt;&gt; mem_funcs&lt;Foo&gt;::list that is a typelist of the member functions of&lt;br /&gt;&gt; foo? That would sure be handy...&lt;br /&gt;&lt;br /&gt;Unfortunately, no language support for introspection is being planned&lt;br /&gt;for C++0x.&lt;br /&gt;&lt;br /&gt;HTH,&lt;br /&gt;&lt;br /&gt;Ganesh&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-4593884468200711260?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/4593884468200711260/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=4593884468200711260' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/4593884468200711260'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/4593884468200711260'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/questions-about-c0x-standard.html' title='Questions about C++0x standard'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-3911256865996593369</id><published>2008-12-04T10:25:00.003-08:00</published><updated>2008-12-04T10:25:58.902-08:00</updated><title type='text'>Hiding a reference to temporary</title><content type='html'>Olivier ha scritto:&lt;br /&gt;&gt; Hi All,&lt;br /&gt;&gt; &lt;br /&gt;&gt; I was playing around with code (the code did consent) and stumbled&lt;br /&gt;&gt; over this :&lt;br /&gt;&gt; &lt;br /&gt;&gt;     #include &lt;iostream&gt;&lt;br /&gt;&gt; &lt;br /&gt;&gt;     char const &amp; hideConstRefToTemporary( char const &amp;c )&lt;br /&gt;&gt;     {&lt;br /&gt;&gt;         return c;&lt;br /&gt;&gt;     }&lt;br /&gt;&gt; &lt;br /&gt;&gt;     char const &amp; getChar1( )&lt;br /&gt;&gt;     {&lt;br /&gt;&gt;         return hideConstRefToTemporary('A');&lt;br /&gt;&gt;     }&lt;br /&gt;&gt; &lt;br /&gt;&gt;     char const &amp; getChar2( )&lt;br /&gt;&gt;     {&lt;br /&gt;&gt;         return 'A';&lt;br /&gt;&gt;     }&lt;br /&gt;&gt; &lt;br /&gt;&gt;     int main( )&lt;br /&gt;&gt;     {&lt;br /&gt;&gt;         std::cout &lt;&lt; getChar() &lt;&lt; "\n";&lt;br /&gt;&gt;         std::cout &lt;&lt; getChar2() &lt;&lt; "\n";&lt;br /&gt;&gt;         return 0;&lt;br /&gt;&gt;     }&lt;br /&gt;&gt; &lt;br /&gt;&gt; If you try and compile this code with gcc (I used version 4.3.2&lt;br /&gt;&gt; 20080827 alpha-testing 1), The compiler will complain of a reference&lt;br /&gt;&gt; to temporary for the function 'getChar2' but not for the function&lt;br /&gt;&gt; 'getChar1'.&lt;br /&gt;&gt; &lt;br /&gt;&gt; Is that a normal behaviour?&lt;br /&gt;&lt;br /&gt;The question is ill-formed. Warnings are not a matter treated by the&lt;br /&gt;standard, they are a quality-of-implementation issue. So a compiler can&lt;br /&gt;emit or fail to emit any kind of warning and still that would be&lt;br /&gt;"normal" behaviour.&lt;br /&gt;&lt;br /&gt;&gt; Is the code valid?&lt;br /&gt;&lt;br /&gt;It depends on your definition of "valid". The code is well-formed, but&lt;br /&gt;the behaviour is undefined, in both cases.&lt;br /&gt;&lt;br /&gt;&gt; Is there any word in the C++ standard that talk about this case?&lt;br /&gt;&lt;br /&gt;No, because it shouldn't. Even the rule that says that returning a&lt;br /&gt;reference to a temporary is bad is never said explicitly AFAIK, but is&lt;br /&gt;implicit in 3.8.&lt;br /&gt;&lt;br /&gt;&gt; I know that temporaries can be bound to a const reference as function&lt;br /&gt;&gt; arguments, but how does that influence the way you use the variable&lt;br /&gt;&gt; representing that temporary ?&lt;br /&gt;&lt;br /&gt;Each time a temporary is bound to a reference, the lifetime of the&lt;br /&gt;temporary is extended to be not shorter than the lifetime of the reference.&lt;br /&gt;&lt;br /&gt;   int f();&lt;br /&gt;&lt;br /&gt;   void g()&lt;br /&gt;   {&lt;br /&gt;      const int&amp; i = f(); // #1: i is bound to the temporary&lt;br /&gt;&lt;br /&gt;      /* ... */&lt;br /&gt;&lt;br /&gt;      // Lifetime of the temporary extended up to the point where i exits&lt;br /&gt;      // from scope. Normally it would have been destroyed at the&lt;br /&gt;      // end of line #1&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;However, this rule has no effect on temporaries bound to function&lt;br /&gt;arguments of reference type, because the argument typically has shorter&lt;br /&gt;lifetime:&lt;br /&gt;&lt;br /&gt;   int f();&lt;br /&gt;   void g(const int&amp; i);&lt;br /&gt;&lt;br /&gt;   void h()&lt;br /&gt;   {&lt;br /&gt;     g(f()); // lifetime of i ends before lifetime of the temporary&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;HTH,&lt;br /&gt;&lt;br /&gt;Ganesh&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-3911256865996593369?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/3911256865996593369/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=3911256865996593369' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/3911256865996593369'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/3911256865996593369'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/hiding-reference-to-temporary_04.html' title='Hiding a reference to temporary'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-8044149691220177433</id><published>2008-12-04T10:25:00.001-08:00</published><updated>2008-12-04T10:25:57.958-08:00</updated><title type='text'>Why the decision to leave typeinfo::name() out of the standard?</title><content type='html'>On Oct 11, 11:59 pm, Sebastian Karlsson&lt;br /&gt;&lt;Sebastian.Karls...@mmorpgs.org&gt; wrote:&lt;br /&gt;&gt; The problem for me is that I need a unique ID which I&lt;br /&gt;&gt; can serialize to be able to later on de serialize from a base pointer,&lt;br /&gt;&gt; it's the same problem which boost::serialize have pretty much.&lt;br /&gt;&lt;br /&gt;Then why not define it yourself?&lt;br /&gt;You could make something like:&lt;br /&gt;template&lt;typename T&gt; struct serialization_traits {&lt;br /&gt;     static const char* const name;&lt;br /&gt;};&lt;br /&gt;and define&lt;br /&gt;template&lt;&gt; serialization_traits&lt;std::basic_string&lt;char,&lt;br /&gt;std::char_traits&lt;char&gt; &gt; &gt;::name = "string";&lt;br /&gt;template&lt;&gt; serialization_traits&lt;std::basic_string&lt;wchar_t,&lt;br /&gt;std::char_traits&lt;wchar_t&gt; &gt; &gt;::name = "unicode_string";&lt;br /&gt;template&lt;&gt; serialization_traits&lt;int&gt;::name = "int";&lt;br /&gt;template&lt;&gt; serialization_traits&lt;SomeNamespace::SomeClass&gt;::name =&lt;br /&gt;"SomeNamespace::SomeClass";&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;This will allow you to define traits only when you need them (and&lt;br /&gt;maybe for basic types) so there is no overhead, and allow you to&lt;br /&gt;control the names given as much as you like (you might want to define&lt;br /&gt;names following a different notation or skipping namespace alltogether&lt;br /&gt;in some cases).&lt;br /&gt;&lt;br /&gt;&gt; The gains from a standardized name() would mainly be for data sharing&lt;br /&gt;&gt; between the same application compiled for different platforms.&lt;br /&gt;&lt;br /&gt;What if the compilations for different platforms use different byte&lt;br /&gt;alignments? How do you serialize between different architectures&lt;br /&gt;(32bit vs. 64bit)?&lt;br /&gt;&lt;br /&gt;I "feel" you're better of with more micromanaging of the serialization&lt;br /&gt;process (but maybe I misunderstood something).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-8044149691220177433?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/8044149691220177433/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=8044149691220177433' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/8044149691220177433'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/8044149691220177433'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/why-decision-to-leave-typeinfoname-out_6369.html' title='Why the decision to leave typeinfo::name() out of the standard?'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-1168149140873050403</id><published>2008-12-04T09:26:00.003-08:00</published><updated>2008-12-04T09:26:22.107-08:00</updated><title type='text'>Are throwing default constructors bad style, and if so, why?</title><content type='html'>On Oct 15, 1:00 am, David Abrahams &lt;d...@boostpro.com&gt; wrote:&lt;br /&gt;&gt; on Mon Oct 13 2008, JoshuaMaurice-AT-gmail.com wrote:&lt;br /&gt;&gt;&lt;br /&gt;&gt;&lt;br /&gt;&gt;&lt;br /&gt;&gt; &gt; On Oct 12, 3:29 pm, David Abrahams &lt;d...@boostpro.com&gt; wrote:&lt;br /&gt;&gt; &gt;&gt; on Sat Oct 04 2008, JoshuaMaurice-AT-gmail.com wrote:&lt;br /&gt;&gt;&lt;br /&gt;&gt; &gt;&gt; &gt; And I really, really like exceptions and RAII. Damn. Disclaimer: This&lt;br /&gt;&gt; &gt;&gt; &gt; doesn't kill all of RAII, but it kills some of it. It effectively&lt;br /&gt;&gt; &gt;&gt; &gt; forces two phase construction for most classes if you want efficient&lt;br /&gt;&gt; &gt;&gt; &gt; code on all platforms, and that makes me very sad.&lt;br /&gt;&gt;&lt;br /&gt;&gt; &gt;&gt; And how, exactly, is two-phase construction going to save you cycles?&lt;br /&gt;&gt;&lt;br /&gt;&gt; &gt; To have single phase construction, constructors must acquires&lt;br /&gt;&gt; &gt; resources. Acquiring resources can fail. User's must be able to know&lt;br /&gt;&gt; &gt; if acquiring a resource fails. The only way I know of doing this is&lt;br /&gt;&gt; &gt; with exceptions or 'zombie' objects. Exceptions carry significant&lt;br /&gt;&gt; &gt; overhead on some implementations, so we're left with zombie objects,&lt;br /&gt;&gt; &gt; and a function to query if it's a zombie.&lt;br /&gt;&gt;&lt;br /&gt;&gt; &gt; Resource r;&lt;br /&gt;&gt; &gt; if (r.isZombie())&lt;br /&gt;&gt; &gt;      return false;&lt;br /&gt;&gt;&lt;br /&gt;&gt; &gt; Otherwise, we can do two phase construction, acquiring resources in&lt;br /&gt;&gt; &gt; init(), and foregoing all exception use. It is very much like the&lt;br /&gt;&gt; &gt; above approach; a zombie state is still required.&lt;br /&gt;&gt;&lt;br /&gt;&gt; &gt; Resource r;&lt;br /&gt;&gt; &gt; if (!r.init())&lt;br /&gt;&gt; &gt;      return false;&lt;br /&gt;&gt;&lt;br /&gt;&gt; &gt; The bad compilers which handle exceptions poorly can be configured to&lt;br /&gt;&gt; &gt; introduce no exception overhead in these two examples as nothing here&lt;br /&gt;&gt; &gt; throws, avoiding the significant penalty of exception overhead.&lt;br /&gt;&gt;&lt;br /&gt;&gt; Maybe I'm missing something.  If the user needs the ultimate effect of&lt;br /&gt;&gt; single-phase construction, he has to invoke both the throwing and&lt;br /&gt;&gt; nonthrowing phases in the 2-phase case.  If both phases could be&lt;br /&gt;&gt; accomplished without throwing, there'd obviously be no advantage to&lt;br /&gt;&gt; 2-phase construction.  Otherwise, phase 2 can throw, and the compiler is&lt;br /&gt;&gt; still going to have to handle the possibility of an exception from phase&lt;br /&gt;&gt; 2, isn't it?&lt;br /&gt;&lt;br /&gt;With single phase construction with constructors, you have to use&lt;br /&gt;exceptions to let the user know you failed to acquire a resource (or&lt;br /&gt;have zombie objects).&lt;br /&gt;&lt;br /&gt;The alternative I proposed for writing "fast" portable code is to&lt;br /&gt;forgo all exception use and instead use error return codes. Thus all&lt;br /&gt;the initialization work cannot be done in the constructor and/or you&lt;br /&gt;have zombie objects. (If you're using a third party library which does&lt;br /&gt;use exceptions, create a small wrapper over it to translate exceptions&lt;br /&gt;to error returns to allow the rest of your program to be exception&lt;br /&gt;handling free, or just immediately catch all exceptions when you call&lt;br /&gt;the third party library, treating exceptions as over-glorified error&lt;br /&gt;return codes like we were all taught not to do.)&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-1168149140873050403?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/1168149140873050403/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=1168149140873050403' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/1168149140873050403'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/1168149140873050403'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/are-throwing-default-constructors-bad_3970.html' title='Are throwing default constructors bad style, and if so, why?'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-7177112622042931465</id><published>2008-12-04T09:26:00.001-08:00</published><updated>2008-12-04T09:26:21.189-08:00</updated><title type='text'>Are throwing default constructors bad style, and if so, why?</title><content type='html'>&lt;br /&gt;on Mon Oct 13 2008, JoshuaMaurice-AT-gmail.com wrote:&lt;br /&gt;&lt;br /&gt;&gt; On Oct 12, 3:29 pm, David Abrahams &lt;d...@boostpro.com&gt; wrote:&lt;br /&gt;&gt;&gt; on Sat Oct 04 2008, JoshuaMaurice-AT-gmail.com wrote:&lt;br /&gt;&gt;&gt;&lt;br /&gt;&gt;&gt; &gt; And I really, really like exceptions and RAII. Damn. Disclaimer: This&lt;br /&gt;&gt;&gt; &gt; doesn't kill all of RAII, but it kills some of it. It effectively&lt;br /&gt;&gt;&gt; &gt; forces two phase construction for most classes if you want efficient&lt;br /&gt;&gt;&gt; &gt; code on all platforms, and that makes me very sad.&lt;br /&gt;&gt;&gt;&lt;br /&gt;&gt;&gt; And how, exactly, is two-phase construction going to save you cycles?&lt;br /&gt;&gt;&lt;br /&gt;&gt; To have single phase construction, constructors must acquires&lt;br /&gt;&gt; resources. Acquiring resources can fail. User's must be able to know&lt;br /&gt;&gt; if acquiring a resource fails. The only way I know of doing this is&lt;br /&gt;&gt; with exceptions or 'zombie' objects. Exceptions carry significant&lt;br /&gt;&gt; overhead on some implementations, so we're left with zombie objects,&lt;br /&gt;&gt; and a function to query if it's a zombie.&lt;br /&gt;&gt;&lt;br /&gt;&gt; Resource r;&lt;br /&gt;&gt; if (r.isZombie())&lt;br /&gt;&gt;      return false;&lt;br /&gt;&gt;&lt;br /&gt;&gt;&lt;br /&gt;&gt; Otherwise, we can do two phase construction, acquiring resources in&lt;br /&gt;&gt; init(), and foregoing all exception use. It is very much like the&lt;br /&gt;&gt; above approach; a zombie state is still required.&lt;br /&gt;&gt;&lt;br /&gt;&gt; Resource r;&lt;br /&gt;&gt; if (!r.init())&lt;br /&gt;&gt;      return false;&lt;br /&gt;&gt;&lt;br /&gt;&gt;&lt;br /&gt;&gt; The bad compilers which handle exceptions poorly can be configured to&lt;br /&gt;&gt; introduce no exception overhead in these two examples as nothing here&lt;br /&gt;&gt; throws, avoiding the significant penalty of exception overhead.&lt;br /&gt;&lt;br /&gt;Maybe I'm missing something.  If the user needs the ultimate effect of&lt;br /&gt;single-phase construction, he has to invoke both the throwing and&lt;br /&gt;nonthrowing phases in the 2-phase case.  If both phases could be&lt;br /&gt;accomplished without throwing, there'd obviously be no advantage to&lt;br /&gt;2-phase construction.  Otherwise, phase 2 can throw, and the compiler is&lt;br /&gt;still going to have to handle the possibility of an exception from phase&lt;br /&gt;2, isn't it?&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;Dave Abrahams&lt;br /&gt;BoostPro Computing&lt;br /&gt;http://www.boostpro.com&lt;br /&gt;&lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-7177112622042931465?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/7177112622042931465/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=7177112622042931465' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/7177112622042931465'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/7177112622042931465'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/are-throwing-default-constructors-bad_04.html' title='Are throwing default constructors bad style, and if so, why?'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-8995029081028511832</id><published>2008-12-04T08:26:00.003-08:00</published><updated>2008-12-04T08:26:09.586-08:00</updated><title type='text'>Hiding a reference to temporary</title><content type='html'>Hi All,&lt;br /&gt;&lt;br /&gt;I was playing around with code (the code did consent) and stumbled&lt;br /&gt;over this :&lt;br /&gt;&lt;br /&gt;    #include &lt;iostream&gt;&lt;br /&gt;&lt;br /&gt;    char const &amp; hideConstRefToTemporary( char const &amp;c )&lt;br /&gt;    {&lt;br /&gt;        return c;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    char const &amp; getChar1( )&lt;br /&gt;    {&lt;br /&gt;        return hideConstRefToTemporary('A');&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    char const &amp; getChar2( )&lt;br /&gt;    {&lt;br /&gt;        return 'A';&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    int main( )&lt;br /&gt;    {&lt;br /&gt;        std::cout &lt;&lt; getChar() &lt;&lt; "\n";&lt;br /&gt;        std::cout &lt;&lt; getChar2() &lt;&lt; "\n";&lt;br /&gt;        return 0;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;If you try and compile this code with gcc (I used version 4.3.2&lt;br /&gt;20080827 alpha-testing 1), The compiler will complain of a reference&lt;br /&gt;to temporary for the function 'getChar2' but not for the function&lt;br /&gt;'getChar1'.&lt;br /&gt;&lt;br /&gt;Is that a normal behaviour?&lt;br /&gt;Is the code valid?&lt;br /&gt;Is there any word in the C++ standard that talk about this case?&lt;br /&gt;&lt;br /&gt;I know that temporaries can be bound to a const reference as function&lt;br /&gt;arguments, but how does that influence the way you use the variable&lt;br /&gt;representing that temporary ?&lt;br /&gt;&lt;br /&gt;Thanks for your insight,&lt;br /&gt;&lt;br /&gt;Olivier.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-8995029081028511832?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/8995029081028511832/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=8995029081028511832' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/8995029081028511832'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/8995029081028511832'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/hiding-reference-to-temporary.html' title='Hiding a reference to temporary'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-7979774176453824119</id><published>2008-12-04T08:26:00.001-08:00</published><updated>2008-12-04T08:26:08.617-08:00</updated><title type='text'>Compiletime-Calculating Prime Numbers</title><content type='html'>Francis Glassborow wrote:&lt;br /&gt;&gt; Well if you insist you could always use a better algorithm but that&lt;br /&gt;&gt; would require much more work at TMP. However I cannot imagine any&lt;br /&gt;&gt; place where it would be useful.&lt;br /&gt;&lt;br /&gt;There are a couple of FFT algorithms which are designed for operand&lt;br /&gt;lengths which are prime, like Rader's Algorithm. An optimized FFT&lt;br /&gt;library could use compile time checks of number theoretic properties to&lt;br /&gt;switch over to an more efficient algorithm for certain inputs.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-7979774176453824119?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/7979774176453824119/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=7979774176453824119' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/7979774176453824119'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/7979774176453824119'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/compiletime-calculating-prime-numbers_6265.html' title='Compiletime-Calculating Prime Numbers'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-1452472724106059393</id><published>2008-12-04T07:27:00.003-08:00</published><updated>2008-12-04T07:27:22.128-08:00</updated><title type='text'>Spurious blank generated in simple Win32 Console app</title><content type='html'>RichardOnRails2 ha scritto:&lt;br /&gt;&gt; Hi,&lt;br /&gt;&gt; &lt;br /&gt;&gt; I'm  a rusty C++ programmer trying to help a new-comer to write a&lt;br /&gt;&gt; permutations-generator.  In my first dozen lines of so manage to write&lt;br /&gt;&gt; an unexpected blank character.&lt;br /&gt;&gt; &lt;br /&gt;&gt; The program and its output are posted at http://www.pastie.org/291679.&lt;br /&gt;&gt; &lt;br /&gt;&gt; The third line " PQR" should be simply "PQR" IMHO.  I've made some&lt;br /&gt;&gt; dumb mistake.  Learning where it is would be most appreciated.&lt;br /&gt;&gt; &lt;br /&gt;&lt;br /&gt;Assuming you work on a platform where the size of pointers is four&lt;br /&gt;bytes, the expression "sizeof(s)/sizeof(*s)" in line 6 always evaluate&lt;br /&gt;at compile time to 4, regardless of the content of the string pointed by&lt;br /&gt;s. In fact sizeof(s) is the size of *the pointer*, which is always four,&lt;br /&gt;and sizeof(*s) is the size of a character, which is always one. As you&lt;br /&gt;use "sizeof(s)/sizeof(*s)" as the size of the field "%*s" you are&lt;br /&gt;actually requesting the blank of padding you see.&lt;br /&gt;&lt;br /&gt;Notice the expression "sizeof(a)/sizeof(*a)" in line 13 is a completely&lt;br /&gt;different case, that unfortunately is also wrong. In fact a is an array&lt;br /&gt;(not a pointer as in the previous case) so the result is the number of&lt;br /&gt;elements of the array, which unfortunately is also 4 and not 3, because&lt;br /&gt;of the implicitly added terminating '\0'.&lt;br /&gt;&lt;br /&gt;Remember: sizeof returns size of a type or object, NOT the length of a&lt;br /&gt;string (be it a literal or not).&lt;br /&gt;&lt;br /&gt;BTW, why did you write:&lt;br /&gt;&lt;br /&gt;    printf("%*s\n", sizeof(s)/sizeof(*s), s);&lt;br /&gt;&lt;br /&gt;where you probably just meant:&lt;br /&gt;&lt;br /&gt;    printf("%s\n", s);&lt;br /&gt;&lt;br /&gt;or, even better:&lt;br /&gt;&lt;br /&gt;    std::cout &lt;&lt; s &lt;&lt; '\n';&lt;br /&gt;&lt;br /&gt;HTH,&lt;br /&gt;&lt;br /&gt;Ganesh&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-1452472724106059393?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/1452472724106059393/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=1452472724106059393' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/1452472724106059393'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/1452472724106059393'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/spurious-blank-generated-in-simple_04.html' title='Spurious blank generated in simple Win32 Console app'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-3589169979850808584</id><published>2008-12-04T07:27:00.001-08:00</published><updated>2008-12-04T07:27:21.246-08:00</updated><title type='text'>Compiletime-Calculating Prime Numbers</title><content type='html'>By compiling the line&lt;br /&gt;&lt;br /&gt;IsPrime&lt;1000&gt;::value;&lt;br /&gt;&lt;br /&gt;Your system is trying to generate, compile and link many thousands&lt;br /&gt;specializations of IsPrime&lt;int i, int j&gt;. I suspect it runs out&lt;br /&gt;of memory somewhere.&lt;br /&gt;&lt;br /&gt;Notice also that:&lt;br /&gt;1) The maximum possible value of an integer factor of j is sqrt(j).&lt;br /&gt;    You need therefore to test only for i &lt;= sqrt(j).&lt;br /&gt;2) Your code does not work if j&lt;=0. You need at least to rise an&lt;br /&gt;exception.&lt;br /&gt;    A better solution is to define j=0 to be prime (or not to be prime:&lt;br /&gt;it is a&lt;br /&gt;    matter of taste) and j&lt;0 to be prime if abs(j) is prime.&lt;br /&gt;&lt;br /&gt;A standard solution to the prime number test problem is to implement&lt;br /&gt;the algorithm in the old-fashioned standard way, not using templates&lt;br /&gt;nor recursion.&lt;br /&gt;&lt;br /&gt;#include &lt;cmath&gt;&lt;br /&gt;#include &lt;iostream&gt;&lt;br /&gt;#include &lt;climits&gt;&lt;br /&gt;&lt;br /&gt;bool isPrime(const int n)&lt;br /&gt;{&lt;br /&gt;   int j = std::abs(n);               // n&lt;0 is prime if abs(n) is&lt;br /&gt;prime&lt;br /&gt;   bool primeSoFar = true;&lt;br /&gt;   int i = int(std::sqrt(j)+0.5);     // the max possible factor of n&lt;br /&gt;is sqrt(n)&lt;br /&gt;   while(primeSoFar &amp;&amp; i&gt;1)           // 0 and 1 are primes by&lt;br /&gt;definition&lt;br /&gt;   {&lt;br /&gt;     if(j%i == 0) primeSoFar=false;&lt;br /&gt;     --i;&lt;br /&gt;   }&lt;br /&gt;   return primeSoFar;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;int main()&lt;br /&gt;{&lt;br /&gt;   std::cout &lt;&lt; isPrime(0)        &lt;&lt; std::endl;   // prime by&lt;br /&gt;definition&lt;br /&gt;   std::cout &lt;&lt; isPrime(1)        &lt;&lt; std::endl;   // prime by&lt;br /&gt;definition&lt;br /&gt;   std::cout &lt;&lt; isPrime(-1)       &lt;&lt; std::endl;   // prime&lt;br /&gt;   std::cout &lt;&lt; isPrime(31)       &lt;&lt; std::endl;   // prime&lt;br /&gt;   std::cout &lt;&lt; isPrime(-31)      &lt;&lt; std::endl;   // prime&lt;br /&gt;   std::cout &lt;&lt; isPrime(100)      &lt;&lt; std::endl;   // not prime&lt;br /&gt;   std::cout &lt;&lt; isPrime(-100)     &lt;&lt; std::endl;   // not prime&lt;br /&gt;   std::cout &lt;&lt; isPrime(1000)     &lt;&lt; std::endl;   // not prime&lt;br /&gt;   std::cout &lt;&lt; isPrime(10000)    &lt;&lt; std::endl;   // not prime&lt;br /&gt;   std::cout &lt;&lt; isPrime(100000)   &lt;&lt; std::endl;   // not prime&lt;br /&gt;   std::cout &lt;&lt; isPrime(1000000)  &lt;&lt; std::endl;   // not prime&lt;br /&gt;   std::cout &lt;&lt; isPrime(10000000) &lt;&lt; std::endl;   // not prime&lt;br /&gt;   std::cout &lt;&lt; isPrime(INT_MAX)  &lt;&lt; std::endl;   // in gcc on a 32-bit&lt;br /&gt;x86 INT_MAX = pow(2,31)-1 = 2147483647 and is prime&lt;br /&gt;   system("pause");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This implementation turns the compile-time complexity of Your code&lt;br /&gt;into run-time complexity.&lt;br /&gt;&lt;br /&gt;By googling You will find other more powerfull and complex solutions&lt;br /&gt;to the primeness-test problem.&lt;br /&gt;&lt;br /&gt;Regards,        Claudio Pacati&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-3589169979850808584?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/3589169979850808584/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=3589169979850808584' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/3589169979850808584'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/3589169979850808584'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/compiletime-calculating-prime-numbers_04.html' title='Compiletime-Calculating Prime Numbers'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-16028823477632036.post-109427484800458245</id><published>2008-12-04T06:26:00.003-08:00</published><updated>2008-12-04T06:26:13.872-08:00</updated><title type='text'>Spurious blank generated in simple Win32 Console app</title><content type='html'>Hi,&lt;br /&gt;&lt;br /&gt;I'm  a rusty C++ programmer trying to help a new-comer to write a&lt;br /&gt;permutations-generator.  In my first dozen lines of so manage to write&lt;br /&gt;an unexpected blank character.&lt;br /&gt;&lt;br /&gt;The program and its output are posted at http://www.pastie.org/291679.&lt;br /&gt;&lt;br /&gt;The third line " PQR" should be simply "PQR" IMHO.  I've made some&lt;br /&gt;dumb mistake.  Learning where it is would be most appreciated.&lt;br /&gt;&lt;br /&gt;Thanks in Advance,&lt;br /&gt;Richard&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;-- &lt;br /&gt;      [ See http://www.gotw.ca/resources/clcm.htm for info about ]&lt;br /&gt;      [ comp.lang.c++.moderated.    First time posters: Do this! ]&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/16028823477632036-109427484800458245?l=cpp-tips-gw.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://cpp-tips-gw.blogspot.com/feeds/109427484800458245/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://www.blogger.com/comment.g?blogID=16028823477632036&amp;postID=109427484800458245' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/109427484800458245'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/16028823477632036/posts/default/109427484800458245'/><link rel='alternate' type='text/html' href='http://cpp-tips-gw.blogspot.com/2008/12/spurious-blank-generated-in-simple.html' title='Spurious blank generated in simple Win32 Console app'/><author><name>Way Greate</name><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00662386575647615929'/></author><thr:total xmlns:thr='http://purl.org/syndication/thread/1.0'>0</thr:total></entry></feed>