相关疑难解决方法(0)

在模板中键入条件

考虑到C++中模板的类型,是否可以仅构建代码的某些部分?这将是湖泊:

#include <iostream>

using namespace std;

template<typename T>
void printType(T param)
{
    #if T == char*
        cout << "char*" << endl;
    #elif T == int
        cout << "int" << endl;
    #else
        cout << "???" << endl;
    #endif
}

int main()
{
    printType("Hello world!");
    printType(1);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ conditional templates

35
推荐指数
4
解决办法
2万
查看次数

标签 统计

c++ ×1

conditional ×1

templates ×1