• <legend id='7xHqP'><style id='7xHqP'><dir id='7xHqP'><q id='7xHqP'></q></dir></style></legend>

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

    <small id='7xHqP'></small><noframes id='7xHqP'>

        <bdo id='7xHqP'></bdo><ul id='7xHqP'></ul>

      1. <tfoot id='7xHqP'></tfoot>

        为什么 vector&lt;bool&gt;::reference 不返回对 bool 的引用?

        Why vectorlt;boolgt;::reference doesn#39;t return reference to bool?(为什么 vectorlt;boolgt;::reference 不返回对 bool 的引用?)

              <tbody id='vmtx8'></tbody>
          1. <legend id='vmtx8'><style id='vmtx8'><dir id='vmtx8'><q id='vmtx8'></q></dir></style></legend>

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

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

                  <bdo id='vmtx8'></bdo><ul id='vmtx8'></ul>
                • 本文介绍了为什么 vector&lt;bool&gt;::reference 不返回对 bool 的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  #include <vector>
                  
                  struct A
                  {
                      void foo(){}
                  };
                  
                  template< typename T >
                  void callIfToggled( bool v1, bool &v2, T & t )
                  {
                      if ( v1 != v2 )
                      {
                          v2 = v1;
                          t.foo();
                      }
                  }
                  
                  int main()
                  {
                      std::vector< bool > v= { false, true, false };
                  
                      const bool f = false;
                      A a;
                  
                      callIfToggled( f, v[0], a );
                      callIfToggled( f, v[1], a );
                      callIfToggled( f, v[2], a );
                  }
                  

                  上面例子的编译产生了下一个错误:

                  The compilation of the example above produces next error :

                  dk2.cpp: In function 'int main()':
                  dk2.cpp:29:28: error: no matching function for call to 'callIfToggled(const bool&, std::vector<bool>::reference, A&)'
                  dk2.cpp:29:28: note: candidate is:
                  dk2.cpp:13:6: note: template<class T> void callIfToggled(bool, bool&, T&)
                  

                  我像这样使用 g++(4.6.1 版)编译:

                  I compiled using g++ (version 4.6.1) like this :

                  g++ -O3 -std=c++0x -Wall -Wextra -pedantic dk2.cpp
                  

                  问题是为什么会这样?vector::reference 不是 bool& 吗?还是编译器的错误?
                  或者,我在尝试一些愚蠢的事情吗?:)

                  The question is why this happens? Is vector<bool>::reference not bool&? Or is it a compiler's bug?
                  Or, am I trying something stupid? :)

                  推荐答案

                  向量专用于 bool.

                  这被认为是标准错误.使用 vector 代替:

                  It is considered a mistake of the std. Use vector<char> instead:

                  template<typename t>
                  struct foo {
                    using type = t;
                  };
                  template<>
                  struct foo<bool> {
                    using type = char;
                  };
                  
                  template<typename t, typename... p>
                  using fixed_vector = std::vector<typename foo<t>::type, p...>;
                  

                  <小时>

                  有时您可能需要引用包含在向量中的布尔值.不幸的是,使用 vector 只能为您提供对字符的引用.如果您确实需要 bool&,请查看 提升容器库.它有一个非特殊版本的 vector.


                  Occasionally you may need references to a bool contained inside the vector. Unfortunately, using vector<char> can only give you references to chars. If you really need bool&, check out the Boost Containers library. It has an unspecialized version of vector<bool>.

                  这篇关于为什么 vector&lt;bool&gt;::reference 不返回对 bool 的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                    <tbody id='U8PTE'></tbody>

                    <tfoot id='U8PTE'></tfoot>

                  1. <legend id='U8PTE'><style id='U8PTE'><dir id='U8PTE'><q id='U8PTE'></q></dir></style></legend>

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

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