guozozo
V2EX  ›  C++

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

  •  
  •   guozozo · Dec 23, 2020 · 2102 views
    This topic created in 2010 days ago, the information mentioned may be changed or developed.
    现有一个模版,如下:
    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 replies    2020-12-23 11:32:21 +08:00
    wutiantong
        1
    wutiantong  
       Dec 23, 2020
    Partial template specialization 偏特化
    kirigaya
        2
    kirigaya  
       Dec 23, 2020
    这个要使用 std::enable_if 来做 SFINAE 。
    看一下这个吧 https://stackoverflow.com/questions/46294229/stdenable-if-with-stdis-same-refuses-to-compile
    guozozo
        3
    guozozo  
    OP
       Dec 23, 2020
    @wutiantong #1 @kirigaya #2 谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5008 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 09:32 · PVG 17:32 · LAX 02:32 · JFK 05:32
    ♥ Do have faith in what you're doing.