相关疑难解决方法(0)

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

// 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;

    }
};
Run Code Online (Sandbox Code Playgroud)

我知道如何编写模板特化并为char类型执行单独的整个类def.

但是,如果我只想在一个代码块中处理所有内容呢?

如何检查T是int还是char?

c++ templates

32
推荐指数
5
解决办法
3万
查看次数

标签 统计

c++ ×1

templates ×1