V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
guozozo
V2EX  ›  C++

有个 C++模版问题想请教大家

  •  
  •   guozozo · 2020-12-23 11:11:52 +08:00 · 1366 次点击
    这是一个创建于 1192 天前的主题,其中的信息可能已经有所发展或是发生改变。
    现有一个模版,如下:
    template<class T1, class T2>
    class X
    {
    //some function
    //构造函数 1
    //构造函数 2
    //构造函数 3
    //函数 1
    //函数 2
    //函数 3
    protected:
    //成员变量: 与 T1,T2 均相关
    }

    现在有这样的需求:
    当 T2 == A 时, 执行构造函数 1,构造函数 2, 函数 1, 函数 2, 禁止执行构造函数 3, 函数 3.
    当 T2 == B 时, 执行构造函数 1,构造函数 3, 函数 1, 函数 3, 禁止执行构造函数 2, 函数 2.

    当前环境只支持到 C++11
    3 条回复    2020-12-23 11:32:21 +08:00
    wutiantong
        1
    wutiantong  
       2020-12-23 11:15:27 +08:00
    Partial template specialization 偏特化
    kirigaya
        2
    kirigaya  
       2020-12-23 11:20:03 +08:00
    这个要使用 std::enable_if 来做 SFINAE 。
    看一下这个吧 https://stackoverflow.com/questions/46294229/stdenable-if-with-stdis-same-refuses-to-compile
    guozozo
        3
    guozozo  
    OP
       2020-12-23 11:32:21 +08:00
    @wutiantong #1 @kirigaya #2 谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5391 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 06:56 · PVG 14:56 · LAX 23:56 · JFK 02:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.