Boost C++ Libraries: Ticket #4649: token_functions.hpp (220) : fix for warning C4127 conditional expression is constant https://svn.boost.org/trac10/ticket/4649 <p> MSVC gives a compiler warning for the following piece of code (starting at line 220) in token_functions.hpp: </p> <hr /> <blockquote> <p> if (sizeof(char_type) == 1) </p> <blockquote> <p> return std::isspace(c) != 0; </p> </blockquote> <p> else </p> <blockquote> <p> return std::iswspace(c) != 0; </p> </blockquote> </blockquote> <hr /> <p> The warning is obvious: </p> <blockquote> <p> warning C4127: conditional expression is constant </p> </blockquote> <p> The warning is causing automated project builds to fail when 'fail on warning' and /W4 (maximum warnings) are set. </p> <p> Please can you make the run-time check for sizeof(char_type)==1 into a compile-time check? A solution could be to change: </p> <hr /> <blockquote> <p> if (sizeof(char_type) == 1) </p> <blockquote> <p> return std::isspace(c) != 0; </p> </blockquote> <p> else </p> <blockquote> <p> return std::iswspace(c) != 0; </p> </blockquote> </blockquote> <hr /> <p> for something along the lines of </p> <hr /> <blockquote> <p> return check_isspace&lt;traits, sizeof(char_type)&gt;::value_for(c); </p> </blockquote> <hr /> <p> where check_isspace is defined as follows: </p> <hr /> <blockquote> <p> template &lt;typename traits, size_t char_type_size&gt; struct check_isspace { </p> <blockquote> <p> typedef typename traits::char_type char_type; </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> static bool value_for(char_type c) { return std::iswspace(c)!=0; } </p> </blockquote> <p> }; </p> </blockquote> <blockquote> <p> template &lt;typename traits&gt; struct check_isspace&lt;traits, 1&gt; { </p> <blockquote> <p> typedef typename traits::char_type char_type; </p> </blockquote> </blockquote> <blockquote> <blockquote> <p> static bool value_for(char_type c) { return std::isspace(c)!=0; } </p> </blockquote> <p> }; </p> </blockquote> <hr /> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4649 Trac 1.4.3 Andrew Macgrego <aamacgregor@…> Wed, 15 Sep 2010 10:40:56 GMT <link>https://svn.boost.org/trac10/ticket/4649#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4649#comment:1</guid> <description> <p> I just realised that the problem also affects ispunct too. </p> <p> Perhaps the following solution? </p> <p> Add: </p> <pre class="wiki">#if !defined(BOOST_NO_CWCTYPE) struct is_space { template &lt;typename char_type&gt; static bool wchar_func(char_type c) { return std::iswspace(c) != 0; } template &lt;typename char_type&gt; static bool char_func(char_type c) { return std::isspace(c) != 0; } }; struct is_punct { template &lt;typename char_type&gt; static bool wchar_func(char_type c) { return std::iswpunct(c) != 0; } template &lt;typename char_type&gt; static bool char_func(char_type c) { return std::ispunct(c) != 0; } }; template &lt;typename function_pair, typename traits, size_t char_type_size&gt; struct check { typedef typename traits::char_type char_type; static bool value_for(char_type c) { return function_pair::wchar_func(c); } }; template &lt;typename function_pair, typename traits&gt; struct check&lt;function_pair, traits, 1&gt; { typedef typename traits::char_type char_type; static bool value_for(char_type c) { return function_pair::char_func(c); } }; #endif </pre><p> And replace: </p> <pre class="wiki"> if (sizeof(char_type) == 1) return std::isspace(c) != 0; else return std::iswspace(c) != 0; </pre><p> with </p> <pre class="wiki"> return check&lt;is_space, traits, sizeof(char_type)&gt;::value_for(c); </pre><p> and replace </p> <pre class="wiki"> if (sizeof(char_type) == 1) return std::ispunct(c) != 0; else return std::iswpunct(c) != 0; </pre><p> with </p> <pre class="wiki"> return check&lt;is_punct, traits, sizeof(char_type)&gt;::value_for(c); </pre> </description> <category>Ticket</category> </item> <item> <author>rvalde@…</author> <pubDate>Mon, 17 Jan 2011 17:53:30 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4649#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4649#comment:2</guid> <description> <p> Looking to submit a bug for this issue, and I found this one. The issue hasn't been solved for 1.45 yet. I'll attached my solution, for if someone likes it. </p> </description> <category>Ticket</category> </item> <item> <author>rvalde@…</author> <pubDate>Mon, 17 Jan 2011 17:54:30 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/4649 https://svn.boost.org/trac10/ticket/4649 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">token_functions.patch</span> </li> </ul> Ticket Marshall Clow Mon, 17 Jan 2011 21:25:06 GMT <link>https://svn.boost.org/trac10/ticket/4649#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4649#comment:3</guid> <description> <p> I attempted to apply your patch; it did not apply cleanly. I merged it in by hand, and found that clang TOT complained about an extra 'typename'. </p> <p> With that taken out, it compiles and passes the tests using gcc 4.2.1 and clang TOT. </p> <p> Adjusted patch attached. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Mon, 17 Jan 2011 21:25:30 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/4649 https://svn.boost.org/trac10/ticket/4649 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">4649-mtc.patch</span> </li> </ul> Ticket anonymous Thu, 20 Jan 2011 12:13:28 GMT <link>https://svn.boost.org/trac10/ticket/4649#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4649#comment:4</guid> <description> <p> Thanks Marshall, sorry for the inconveniences caused by a bad patch. </p> <p> I think the adjusted patch is missing something. There's a remaining const expression "if (sizeof(char)==1)" in traits_extension::ispunct that should be removed. </p> <pre class="wiki"> static bool ispunct(char_type c) { #if !defined(BOOST_NO_CWCTYPE) return traits_extension_details&lt;traits, sizeof(char_type)&gt;::ispunct(c); #else return static_cast&lt; unsigned &gt;(c) &lt;= 255 &amp;&amp; std::ispunct(c) != 0; #endif } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Thu, 20 Jan 2011 14:31:39 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4649#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4649#comment:5</guid> <description> <p> Well, crud. You are correct. Updated patch attached. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Thu, 20 Jan 2011 14:31:58 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/4649 https://svn.boost.org/trac10/ticket/4649 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">4649-mtc-2.patch</span> </li> </ul> Ticket Marshall Clow Tue, 05 Apr 2011 17:33:53 GMT <link>https://svn.boost.org/trac10/ticket/4649#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4649#comment:6</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/71006" title="Applied patch; Refs #4649">[71006]</a>) Applied patch; Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4649" title="#4649: Bugs: token_functions.hpp (220) : fix for warning C4127 conditional ... (closed: fixed)">#4649</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Marshall Clow</dc:creator> <pubDate>Mon, 11 Apr 2011 13:21:19 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4649#comment:7 https://svn.boost.org/trac10/ticket/4649#comment:7 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/71185" title="Merge tokenizer fixes to release. Fixes #4649">[71185]</a>) Merge tokenizer fixes to release. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4649" title="#4649: Bugs: token_functions.hpp (220) : fix for warning C4127 conditional ... (closed: fixed)">#4649</a> </p> Ticket