<small id='TGYoL'></small><noframes id='TGYoL'>

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

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

        <tfoot id='TGYoL'></tfoot>
        <legend id='TGYoL'><style id='TGYoL'><dir id='TGYoL'><q id='TGYoL'></q></dir></style></legend>

        boost::variant - 为什么是“const char*";转换为“布尔"?

        时间:2023-07-19
          <bdo id='KiJ2t'></bdo><ul id='KiJ2t'></ul>

        • <legend id='KiJ2t'><style id='KiJ2t'><dir id='KiJ2t'><q id='KiJ2t'></q></dir></style></legend>
            <tfoot id='KiJ2t'></tfoot>
                <tbody id='KiJ2t'></tbody>

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

                  <small id='KiJ2t'></small><noframes id='KiJ2t'>

                  本文介绍了boost::variant - 为什么是“const char*";转换为“布尔"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我已经声明了一个 boost::variant,它接受三种类型:stringboolint.以下代码显示我的变体接受 const char* 并将其转换为 bool.boost::variant 接受和转换不在其列表中的类型是正常行为吗?

                  I have declared a boost::variant which accepts three types: string, bool and int. The following code is showing that my variant accepts const char* and converts it to bool. Is it a normal behavior for boost::variant to accept and convert types not on its list?

                  #include <iostream>
                  #include "boost/variant/variant.hpp"
                  #include "boost/variant/apply_visitor.hpp"
                  
                  using namespace std;
                  using namespace boost;
                  
                  typedef variant<string, bool, int> MyVariant;
                  
                  class TestVariant
                      : public boost::static_visitor<>
                  {
                  public:
                      void operator()(string &v) const
                      {
                          cout << "type: string -> " << v << endl;
                      }
                      template<typename U>
                      void operator()(U &v)const
                      {
                          cout << "type: other -> " << v << endl;
                      }
                  };
                  
                  int main(int argc, char **argv) 
                  {
                      MyVariant s1 = "some string";
                      apply_visitor(TestVariant(), s1);
                  
                      MyVariant s2 = string("some string");
                      apply_visitor(TestVariant(), s2);
                  
                      return 0;
                  }
                  

                  输出:

                  类型:其他 -> 1
                  类型:字符串 -> 一些字符串

                  type: other -> 1
                  type: string -> some string

                  如果我从 MyVariant 中删除 bool 类型并将其更改为:

                  If I remove the bool type from MyVariant and change it to this:

                  typedef variant<string, int> MyVariant;
                  

                  const char* 不再转换为 bool.这次它被转换为 string,这是新的输出:

                  const char* is no more converted to bool. This time it's converted to string and this is the new output:

                  类型:字符串 -> 一些字符串
                  类型:字符串 -> 一些字符串

                  type: string -> some string
                  type: string -> some string

                  这表明variant 尝试先将其他类型转换为bool,然后再转换为string.如果类型转换是不可避免的并且应该总是发生,有什么方法可以将转换为 string 赋予更高的优先级?

                  This indicates that variant tries to convert other types first to bool and then to string. If the type conversion is something inevitable and should always happen, is there any way to give conversion to string a higher priority?

                  推荐答案

                  我不认为这与 boost::variant 有什么特别关系,而是关于重载解析选择了哪个构造函数.重载函数也会发生同样的事情:

                  I don't think this is anything particularly to do with boost::variant, it's about which constructor gets selected by overload resolution. The same thing happens with an overloaded function:

                  #include <iostream>
                  #include <string>
                  
                  void foo(bool) {
                      std::cout << "bool
                  ";
                  }
                  
                  void foo(std::string) {
                      std::cout << "string
                  ";
                  }
                  
                  int main() {
                      foo("hi");
                  }
                  

                  输出:

                  bool
                  

                  我不知道有什么方法可以更改 Variant 的构造函数

                  I don't know of a way to change what constructors a Variant has [edit: as James says, you can write another class that uses the Variant in its implementation. Then you can provide a const char* constructor that does the right thing.]

                  也许您可以更改 Variant 中的类型.另一个重载示例:

                  Maybe you could change the types in the Variant. Another overloading example:

                  struct MyBool {
                      bool val;
                      explicit MyBool(bool val) : val(val) {}
                  };
                  
                  void bar(MyBool) {
                      std::cout << "bool
                  ";
                  }
                  
                  void bar(const std::string &) {
                      std::cout << "string
                  ";
                  }
                  
                  int main() {
                      bar("hi");
                  }
                  

                  输出:

                  string
                  

                  不幸的是,现在您必须编写 bar(MyBool(true)) 而不是 foo(true).更糟糕的是,对于带有 string/bool/int 的变体,如果您只是将其更改为 string/MyBool/int 的变体,则 MyVariant(true) 将调用 int 构造函数.

                  Unfortunately now you have to write bar(MyBool(true)) instead of foo(true). Even worse in the case of your variant with string/bool/int, if you just change it to a variant of string/MyBool/int then MyVariant(true) would call the int constructor.

                  这篇关于boost::variant - 为什么是“const char*";转换为“布尔"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 boost 解析日期时间字符串:使用一位数的小时格式 下一篇:在完成工作后立即删除 boost::thread 对象的最佳方法是什么?

                  相关文章

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

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

                    1. <small id='C8XFB'></small><noframes id='C8XFB'>