c++ 像“_M_construct”这样的函数定义在哪里

20jt8wwn  于 2023-05-02  发布在  其他
关注(0)|答案(1)|浏览(152)

我在Debian上拆解了一个自己写的非常简单的C++程序。它涉及一些std::string操作。在反汇编代码中,我可以看到许多引用如下所示:

<void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char const*>(char const*, char const*, std::forward_iterator_tag)>

我相信这是一些模板函数创建的具体字符串构造函数,用于初始化字符串。然而,反汇编的代码似乎并没有提供模板函数定义的确切位置的线索。
我查了/usr/include/c++/10/string,没找到什么。我还问了ChatGPT,它说它是在libstdc++-10-dev中定义的,我发出apt-get source libstdc++-10-dev来获取它建议的源代码,但我只找到了下面这样的东西,这不是我真正想要的(例如,在debian/libcc1-0.symbols中,我可以看到四行,只有那些字符串,但没有函数体):

18 results - 4 files

debian/libcc1-0.symbols:
   8   (optional=abi_c++17)_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EE17_M_realloc_insertIJS5_EEEvN9__gnu_cxx17__normal_iteratorIPS5_S7_EEDpOT_@Base 8
   9:  (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag@Base 8
  10:  (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPcEEvT_S7_St20forward_iterator_tag@Base 8
  11   (optional=abi_c++11)_ZNSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS5_EED1Ev@Base 10

debian/libstdc++6.symbols.32bit.cxx11:
  116   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE11_M_capacityEj@GLIBCXX_3.4.21 5.2
  117:  (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructEjc@GLIBCXX_3.4.21 5.2
  118   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE13_M_set_lengthEj@GLIBCXX_3.4.21 5.2
  119   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE14_M_replace_auxEjjjc@GLIBCXX_3.4.21 5.2
  120:  (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE18_M_construct_aux_2Ejc@GLIBCXX_3.4.21 5.2
  121   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE2atEj@GLIBCXX_3.4.21 5.2

  170   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE11_M_capacityEj@GLIBCXX_3.4.21 5.2
  171:  (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE12_M_constructEjw@GLIBCXX_3.4.21 5.2
  172   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE13_M_set_lengthEj@GLIBCXX_3.4.21 5.2
  173   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE14_M_replace_auxEjjjw@GLIBCXX_3.4.21 5.2
  174:  (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE18_M_construct_aux_2Ejw@GLIBCXX_3.4.21 5.2
  175   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE2atEj@GLIBCXX_3.4.21 5.2

debian/libstdc++6.symbols.64bit.cxx11:
  116   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE11_M_capacityEm@GLIBCXX_3.4.21 5.2
  117:  (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructEmc@GLIBCXX_3.4.21 5.2
  118   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE13_M_set_lengthEm@GLIBCXX_3.4.21 5.2
  119   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE14_M_replace_auxEmmmc@GLIBCXX_3.4.21 5.2
  120:  (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE18_M_construct_aux_2Emc@GLIBCXX_3.4.21 5.2
  121   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE2atEm@GLIBCXX_3.4.21 5.2

  170   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE11_M_capacityEm@GLIBCXX_3.4.21 5.2
  171:  (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE12_M_constructEmw@GLIBCXX_3.4.21 5.2
  172   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE13_M_set_lengthEm@GLIBCXX_3.4.21 5.2
  173   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE14_M_replace_auxEmmmw@GLIBCXX_3.4.21 5.2
  174:  (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE18_M_construct_aux_2Emw@GLIBCXX_3.4.21 5.2
  175   (optional=abi_c++11)_ZNSt7__cxx1112basic_stringIwSt11char_traitsIwESaIwEE2atEm@GLIBCXX_3.4.21 5.2

所以问题是:
1.像:_M_construct<char const*>(char const*, char const*, std::forward_iterator_tag)这样的函数真的存在吗?
1.如果是这样,我在哪里可以找到他们?
1.如果没有,我怎么知道是哪些模板生成的?

oaxa6hgo

oaxa6hgo1#

即使我晚了一点,我也会尽量回答你的问题。
您所展示的helper函数是以下函数的特殊化,在basic_string.h文件中声明:

// For forward_iterators up to random_access_iterators, used for
// string::iterator, _CharT*, etc.
template<typename _FwdIterator>
  _GLIBCXX20_CONSTEXPR
  void
  _M_construct(_FwdIterator __beg, _FwdIterator __end,
std::forward_iterator_tag);

它的实现可以在basic_string.tcc文件中找到:

template<typename _CharT, typename _Traits, typename _Alloc>
     template<typename _InIterator>
       _GLIBCXX20_CONSTEXPR
       void
       basic_string<_CharT, _Traits, _Alloc> ::
       _M_construct(_InIterator __beg, _InIterator __end,
std::forward_iterator_tag)
         {
           size_type __dnew = static_cast<size_type>(std::distance(__beg, __end));

           if (__dnew > size_type(_S_local_capacity))
             {
                _M_data(_M_create(__dnew, size_type(0)));
                _M_capacity(__dnew);
             }
           else
                _M_use_local_data();

           // Check for out_of_range and length_error exceptions.
           struct _Guard
             {
               _GLIBCXX20_CONSTEXPR
               explicit _Guard(basic_string* __s) : _M_guarded(__s) { }

               _GLIBCXX20_CONSTEXPR
               ~_Guard() { if (_M_guarded) _M_guarded->_M_dispose(); }

               basic_string* _M_guarded;
             } __guard(this);

           this->_S_copy_chars(_M_data(), __beg, __end);

           __guard._M_guarded = 0;

           _M_set_length(__dnew);
         }

相关问题