Boost C++ Libraries: Ticket #7929: boost::lexical_cast< std::string, std::wstring > gives a compiler error. https://svn.boost.org/trac10/ticket/7929 <p> This causes a problem with <code>boost::program_options::typed_value&lt; std::wstring, wchar_t &gt;::default_value( const std::wstring&amp; v )</code> which attempts to use lexical_cast to convert <code>v</code> into a <code>std::string</code>. Using <code>lexical_cast</code> to convert a wide string to a normal one may be debatable, but program_options should not give a compiler error on a valid use case such as the one shown below. Luckily you can work around this by providing an explicit <code>std::string</code> version or not using the <code>default_value</code> method at all. </p> <h4 class="section" id="lexical_casterror">lexical_cast error</h4> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;boost/lexical_cast.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp"></span> <span class="kt">int</span> <span class="nf">main</span><span class="p">(</span> <span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">wchar_t</span><span class="o">*</span> <span class="n">argv</span><span class="p">[]</span> <span class="p">){</span> <span class="n">std</span><span class="o">::</span><span class="n">wstring</span> <span class="n">wstr</span> <span class="o">=</span> <span class="sa">L</span><span class="s">&quot;some string&quot;</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">str</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">lexical_cast</span><span class="o">&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="o">&gt;</span><span class="p">(</span> <span class="n">wstr</span> <span class="p">);</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><h4 class="section" id="program_optionserror">program_options error</h4> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;boost/program_options.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp"></span> <span class="kt">int</span> <span class="nf">main</span><span class="p">(</span> <span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="kt">wchar_t</span><span class="o">*</span> <span class="n">argv</span><span class="p">[]</span> <span class="p">){</span> <span class="k">namespace</span> <span class="n">po</span> <span class="o">=</span> <span class="n">boost</span><span class="o">::</span><span class="n">program_options</span><span class="p">;</span> <span class="n">po</span><span class="o">::</span><span class="n">options_description</span> <span class="n">opts</span><span class="p">(</span> <span class="s">&quot;Some opts&quot;</span> <span class="p">);</span> <span class="n">std</span><span class="o">::</span><span class="n">wstring</span> <span class="n">str</span> <span class="o">=</span> <span class="sa">L</span><span class="s">&quot;&quot;</span><span class="p">;</span> <span class="n">opts</span><span class="p">.</span><span class="n">add_options</span><span class="p">()</span> <span class="p">(</span> <span class="s">&quot;o&quot;</span><span class="p">,</span> <span class="n">po</span><span class="o">::</span><span class="n">wvalue</span><span class="o">&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">wstring</span> <span class="o">&gt;</span><span class="p">(</span> <span class="o">&amp;</span><span class="n">str</span> <span class="p">)</span> <span class="o">-&gt;</span><span class="n">default_value</span><span class="p">(</span> <span class="sa">L</span><span class="s">&quot;default value&quot;</span> <span class="p">),</span> <span class="c1">// This line causes error.</span> <span class="s">&quot;an option&quot;</span> <span class="p">);</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div></div> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7929 Trac 1.4.3 Antony Polukhin Mon, 28 Jan 2013 18:48:09 GMT owner, component changed; cc set https://svn.boost.org/trac10/ticket/7929#comment:1 https://svn.boost.org/trac10/ticket/7929#comment:1 <ul> <li><strong>cc</strong> <span class="trac-author">antoshkka@…</span> added </li> <li><strong>owner</strong> changed from <span class="trac-author">Antony Polukhin</span> to <span class="trac-author">Vladimir Prus</span> </li> <li><strong>component</strong> <span class="trac-field-old">lexical_cast</span> → <span class="trac-field-new">program_options</span> </li> </ul> <p> I'm afraid that this is not a bug of lexical_cast. Lexical_cast shall work like a std::stringstream/std::wstringstream, so the first example is equal to the following: </p> <pre class="wiki">#include &lt;sstream&gt; int main() { std::wstringstream wss(L"some string"); std::string str; wss &gt;&gt; str; // Compilation error return 0; } </pre><p> And this code must not compile (if I'm wrong and my current compiler fail to compile a correct code - please tell me about it). </p> <p> So, this looks more like a program_options bug. </p> Ticket nate@… Tue, 29 Jan 2013 06:29:22 GMT <link>https://svn.boost.org/trac10/ticket/7929#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7929#comment:2</guid> <description> <p> I'm not sure I agree that <code>lexical_cast</code> is just supposed to be a wrapper around <code>std::stringstream</code> when its own documentation states: </p> <blockquote> <p> The lexical_cast function template offers a convenient and consistent form for supporting common conversions to and from arbitrary types when they are represented as text. </p> </blockquote> <p> That, to me, sounds like it should work for converting <code>std::wstring</code> to <code>std::string</code>. It is an common conversion dealing with arbitrary types and text. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Antony Polukhin</dc:creator> <pubDate>Fri, 01 Feb 2013 14:00:24 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7929#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7929#comment:3</guid> <description> <p> To be honest, I also miss that functionality sometimes. </p> <p> But before implementing it, I need to </p> <ul><li>fix existing issues with std::locale on some compilers </li><li>determinate a portable and reliable way to do narrowing of characters (it looks almost impossible to me) </li></ul><p> So this won't be fixed in nearest future. And I'm afraid that Boost.Locale shall be used in such cases... </p> </description> <category>Ticket</category> </item> <item> <author>nate@…</author> <pubDate>Wed, 12 Jun 2013 19:07:09 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/7929#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/7929#comment:4</guid> <description> <p> Perhaps one can look to the standard library for inspiration? In the <code>locale</code> header there is the <a class="ext-link" href="http://en.cppreference.com/w/cpp/locale/codecvt"><span class="icon">​</span>std::codecvt</a> class which can be used with <a class="ext-link" href="http://en.cppreference.com/w/cpp/locale/wstring_convert"><span class="icon">​</span>std::wstring_convert</a> to perform convertions from one encoding to another. </p> <p> Below is an example using the C++11 class <a class="ext-link" href="http://en.cppreference.com/w/cpp/locale/codecvt_utf8_utf16"><span class="icon">​</span>std::codecvt_utf8_utf16</a> to convert a UTF-16 <code>std::wstring</code> to a UTF-8 encoded <code>std::string</code>. I am using this in my application to great success so far. </p> <pre class="wiki"> std::wstring wideString; std::wstring_convert&lt; std::codecvt_utf8_utf16&lt; std::wstring::value_type &gt;, std::wstring::value_type &gt; utf16conv; std::string narrowString = utf16conv.to_bytes( wideString ); </pre><p> I hope this helps to show a potential way to narrow characters. </p> </description> <category>Ticket</category> </item> </channel> </rss>