• <small id='aT77C'></small><noframes id='aT77C'>

      1. <i id='aT77C'><tr id='aT77C'><dt id='aT77C'><q id='aT77C'><span id='aT77C'><b id='aT77C'><form id='aT77C'><ins id='aT77C'></ins><ul id='aT77C'></ul><sub id='aT77C'></sub></form><legend id='aT77C'></legend><bdo id='aT77C'><pre id='aT77C'><center id='aT77C'></center></pre></bdo></b><th id='aT77C'></th></span></q></dt></tr></i><div id='aT77C'><tfoot id='aT77C'></tfoot><dl id='aT77C'><fieldset id='aT77C'></fieldset></dl></div>
        <tfoot id='aT77C'></tfoot>

      2. <legend id='aT77C'><style id='aT77C'><dir id='aT77C'><q id='aT77C'></q></dir></style></legend>
        • <bdo id='aT77C'></bdo><ul id='aT77C'></ul>

        为什么编译器选择 bool 而不是 string 来隐式类型转

        时间:2023-10-06
        <legend id='qa7Xg'><style id='qa7Xg'><dir id='qa7Xg'><q id='qa7Xg'></q></dir></style></legend>
        <tfoot id='qa7Xg'></tfoot>
          1. <small id='qa7Xg'></small><noframes id='qa7Xg'>

                <i id='qa7Xg'><tr id='qa7Xg'><dt id='qa7Xg'><q id='qa7Xg'><span id='qa7Xg'><b id='qa7Xg'><form id='qa7Xg'><ins id='qa7Xg'></ins><ul id='qa7Xg'></ul><sub id='qa7Xg'></sub></form><legend id='qa7Xg'></legend><bdo id='qa7Xg'><pre id='qa7Xg'><center id='qa7Xg'></center></pre></bdo></b><th id='qa7Xg'></th></span></q></dt></tr></i><div id='qa7Xg'><tfoot id='qa7Xg'></tfoot><dl id='qa7Xg'><fieldset id='qa7Xg'></fieldset></dl></div>
                  <bdo id='qa7Xg'></bdo><ul id='qa7Xg'></ul>
                    <tbody id='qa7Xg'></tbody>
                • 本文介绍了为什么编译器选择 bool 而不是 string 来隐式类型转换 L""?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  最近引入了一个方法的重载,应用程序开始失败.终于追查到了,新方法在我没想到的地方被调用了.

                  Having recently introduced an overload of a method the application started to fail. Finally tracking it down, the new method is being called where I did not expect it to be.

                  我们有

                  setValue( const std::wstring& name, const std::wstring& value );
                  
                  std::wstring avalue( func() );
                  setValue( L"string", avalue );
                  std::wstring bvalue( func2() ? L"true", L"false" );
                  setValue( L"bool", bvalue );
                  setValue( L"empty", L"" );
                  

                  它已更改,以便在存储 bool 值时我们使用相同的字符串(字符串的内部数据存储)

                  It was changed so that when a bool value is stored we use the same strings (internal data storage of strings)

                  setValue( const std::wstring& name, const std::wstring& value );
                  setValue( const std::wstring& name, const bool& value );
                  
                  std::wstring avalue( func() );
                  setValue( L"string", avalue );
                  setValue( L"bool", func2() );
                  setValue( L"empty", L"" ); << --- this FAILS!?!
                  

                  L"" 的问题在于它是隐式转换的,以前它很高兴成为 std::wstring,但它不喜欢成为 bool.MSVC 编译器不会抱怨或发出警告,所以我担心即使我修复"了 setValue( L"empty", L"" );成为

                  The problem with L"" is that it is implicitly casting and previously it was happy to be a std::wstring, but not it prefers to be a bool. The MSVC compiler does not complain, or raise warning, so I'm worried that even if I "fix" the setValue( L"empty", L"" ); to be

                  setValue( L"empty", std::wstring() );
                  

                  其他人可能会稍后来,只需使用 setValue( L"empty", L"" );并且必须再次追踪此问题.

                  somebody else may come later and simply use setValue( L"empty", L"" ); and have to track down this issue again.

                  我们想在方法上使用显式,但它不是此用法的有效关键字.有没有办法让编译器抱怨这个,或者以其他方式防止这个问题?否则,我正在考虑更改采用 bool 的方法的名称,以确保它不会做出错误的猜测.

                  We thought to use explicit on the method but it is not a valid keyword for this usage. Is there some way to get the compiler to complain about this, or otherwise prevent the issue? Otherwise I'm thinking to change the name of the method which takes a bool to ensure it can't make an incorrect guess.

                  推荐答案

                  一、此问题的原因:C++标准[over.ics.rank]/2.11 定义了转换序列的顺序.它说用户定义的转换序列比标准转换序列更糟糕.在您的情况下,字符串文字会进行布尔转换(在 4.12 中定义.这是标准转换).它不使用用户定义的到 std::wstring 的转换,如果它采用其他重载,则需要这种转换.

                  First, the cause of this issue: C++ Standard [over.ics.rank]/2.11 defines an order for conversion sequences. It says that a user defined conversion sequence is worse than a standard conversion sequence. What happens in your case is that the string literal undergoes a boolean-conversion (defined at 4.12. This is a standard conversion). It does not use the user defined conversion to std::wstring which would be needed if it took the other overload.

                  我建议简单地更改重载之一的名称或添加直接接受字符串文字的重载(使用参数类型 wchar_t const*).

                  I would recommend to simply change the name of one of the overloads or adding an overload that accepts the string literal directly (using parameter type wchar_t const*).

                  1)

                  比较隐式转换序列的基本形式时(如[over.best.ics]中所定义)

                  When comparing the basic forms of implicit conversion sequences (as defined in [over.best.ics])

                  (2.1) 标准转换序列是比用户定义的转换序列或省略号转换序列更好的转换序列,并且
                  (2.2) 自定义转换序列比省略号转换序列更好.

                  (2.1) a standard conversion sequence is a better conversion sequence than a user-defined conversion sequence or an ellipsis conversion sequence, and
                  (2.2) a user-defined conversion sequence is a better conversion sequence than an ellipsis conversion sequence.

                  这篇关于为什么编译器选择 bool 而不是 string 来隐式类型转换 L""?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何强制 C++ 编译器使用特定的 CRT 版本? 下一篇:将 bind1st 用于通过引用获取参数的方法

                  相关文章

                  最新文章

                    <bdo id='SZ1x1'></bdo><ul id='SZ1x1'></ul>

                      <tfoot id='SZ1x1'></tfoot>

                      <legend id='SZ1x1'><style id='SZ1x1'><dir id='SZ1x1'><q id='SZ1x1'></q></dir></style></legend>
                    1. <small id='SZ1x1'></small><noframes id='SZ1x1'>

                      <i id='SZ1x1'><tr id='SZ1x1'><dt id='SZ1x1'><q id='SZ1x1'><span id='SZ1x1'><b id='SZ1x1'><form id='SZ1x1'><ins id='SZ1x1'></ins><ul id='SZ1x1'></ul><sub id='SZ1x1'></sub></form><legend id='SZ1x1'></legend><bdo id='SZ1x1'><pre id='SZ1x1'><center id='SZ1x1'></center></pre></bdo></b><th id='SZ1x1'></th></span></q></dt></tr></i><div id='SZ1x1'><tfoot id='SZ1x1'></tfoot><dl id='SZ1x1'><fieldset id='SZ1x1'></fieldset></dl></div>