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

  • <legend id='xoxma'><style id='xoxma'><dir id='xoxma'><q id='xoxma'></q></dir></style></legend>

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

          <bdo id='xoxma'></bdo><ul id='xoxma'></ul>
      1. 如何在 C++ 中将 typename T 转换为字符串

        时间:2023-05-23

        <legend id='J23mz'><style id='J23mz'><dir id='J23mz'><q id='J23mz'></q></dir></style></legend>

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

        1. <i id='J23mz'><tr id='J23mz'><dt id='J23mz'><q id='J23mz'><span id='J23mz'><b id='J23mz'><form id='J23mz'><ins id='J23mz'></ins><ul id='J23mz'></ul><sub id='J23mz'></sub></form><legend id='J23mz'></legend><bdo id='J23mz'><pre id='J23mz'><center id='J23mz'></center></pre></bdo></b><th id='J23mz'></th></span></q></dt></tr></i><div id='J23mz'><tfoot id='J23mz'></tfoot><dl id='J23mz'><fieldset id='J23mz'></fieldset></dl></div>
              <tfoot id='J23mz'></tfoot>
                  <tbody id='J23mz'></tbody>
                  <bdo id='J23mz'></bdo><ul id='J23mz'></ul>
                  本文介绍了如何在 C++ 中将 typename T 转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  在 C++ 中使用模板时,我遇到了将 typename T 转换为字符串的问题.例如:

                  While playing with templates in c++ I encountered a problem converting typename T to string. For example:

                  template <typename T>
                  class Matrix {
                     public:
                          Matrix() {
                             //my_type = string type of T. i.e. if T is char. I want my_type to be "char".
                          }
                     string my_type;
                  }
                  

                  如何将 T 转换为表示 T 是什么的字符串.

                  How do I convert T to a string that says what T is.

                  注意:我只是在玩,所以请不要担心什么时候可能需要这样的东西.

                  Note: I'm just playing around so please do not worry about when one might need such a thing.

                  推荐答案

                  没有针对此的内置机制.

                  There is no built-in mechanism for this.

                  typeid(T)::name() 可以提供一些信息,但标准并没有要求这个字符串是人类可读的;只是它必须对每种类型都不同.(例如 Microsoft Visual C++ 使用人类可读的字符串;GCC 不使用.)

                  typeid(T)::name() can give some info, but the standard does not mandate this string to be human-readable; just that it has to be distinct for each type. (E.x. Microsoft Visual C++ uses human-readable strings; GCC does not.)

                  不过,您可以构建自己的系统.例如,基于特征.像这样:

                  You can build your own system though. For example, traits-based. Something like this:

                  // default implementation
                  template <typename T>
                  struct TypeName
                  {
                      static const char* Get()
                      {
                          return typeid(T).name();
                      }
                  };
                  
                  // a specialization for each type of those you want to support
                  // and don't like the string returned by typeid
                  template <>
                  struct TypeName<int>
                  {
                      static const char* Get()
                      {
                          return "int";
                      }
                  };
                  
                  // usage:
                  const char* name = TypeName<MyType>::Get();
                  

                  这篇关于如何在 C++ 中将 typename T 转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:std::enable_if :参数与模板参数 下一篇:友元声明声明了一个非模板函数

                  相关文章

                  最新文章

                  • <bdo id='TCNMT'></bdo><ul id='TCNMT'></ul>

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

                  <tfoot id='TCNMT'></tfoot>

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

                      <legend id='TCNMT'><style id='TCNMT'><dir id='TCNMT'><q id='TCNMT'></q></dir></style></legend>