栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ int2Type&type2Type

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ int2Type&type2Type

#include 


// 编译期就能确定函数调用,主要是编译器不会去编译一个未被用到的template函数
// 只会做语法检查,在template代码中大部分需要在编译器期间进行dispatch分发操作




// 可以使用int2Type进行进行类型重载
template class Int2Type {
    enum {value = T};
};

class ST{};

template
class NiftyContainer{
public:
    void DoSomeThing(T* obj) {
        this->DoSomeThing(obj, Int2Type());
    }
private:
    // Int2Type是不同类型,因此函数可以正常重载
    void DoSomeThing(T* obj, Int2Type) {
        std::cout<<"polymophic"<) {
        std::cout<<"no polymophic"<
    ST st;
    NiftyContainer nt; // DoSomeThing(T* obj, Int2Type)不会被编译,没有用到
    nt.DoSomeThing(&st);

}




// template
// void Print(T* t, Q q) {
//     std::cout<<"template function 原型"<
// void Print(T* t, int i) {
//     std::cout<<"template function 偏特化"<' is not allowed


// 这是模板函数重载
template
void Print(T* t, Q q) {
    std::cout<<"template function 重载1"<
void Print(T* t, int i) {
    std::cout<<"template function 重载2"<
    Print("hello", 2); // 重载2
    Print("hello", "world");// 重载1
}

// 使用type2Type映射效果更好
template class Type2Type{ typedef T NewDefinedType;};

template
T* CreateObj(Q q, Type2Type) {
    // 函数原型
    std::cout<
int* CreateObj(Q q, Type2Type) {
    std::cout<
   std::string* pstr = CreateObj("hello", Type2Type()); // hello
   int * i = CreateObj(10, Type2Type());  // 10
}
int main() {
    Test();
    
  return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1018631.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号