• <tfoot id='HOm5c'></tfoot>

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

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

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

        c++ - 如何在c ++模板中执行if else依赖类型?

        时间:2023-05-25
            <tfoot id='K5sni'></tfoot>

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

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

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

                    <tbody id='K5sni'></tbody>
                  本文介绍了c++ - 如何在c ++模板中执行if else依赖类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..
                  // template specialization
                  #include <iostream>
                  using namespace std;
                  
                  // class template:
                  template <class T>
                  class mycontainer {
                      T element;
                    public:
                      mycontainer (T arg) {element=arg;}
                      T increase () {
                  
                      //if(T.type==int)//how to do this or something similar?
                      //do this if an int
                      return ++element;
                  
                      //if(T.type==char)
                       //if ((element>='a')&&(element<='z'))
                        //element+='A'-'a';
                        //return element;
                  
                      }
                  };
                  

                  我知道如何编写模板特化并为 char 类型做一个单独的整个类定义.

                  I know how to write a template specialization and do a separate whole class def just for the char type.

                  但是如果我想在一个代码块中处理所有事情怎么办?

                  But what if I wanted to handle everything in just one block of code?

                  如何检查 T 是 int 还是 char?

                  How can I check if T is an int or a char?

                  推荐答案

                  你可以使用 typeid:

                  if (typeid(T) == typeid(int))
                  

                  或者你可以使用 std::is_same 类型特征:

                  Or you could use the std::is_same type trait:

                  if (std::is_same<T, int>::value)
                  

                  这篇关于c++ - 如何在c ++模板中执行if else依赖类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:std::enable_if 如何工作? 下一篇:专用模板类的静态成员初始化

                  相关文章

                  最新文章

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

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

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