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

2020-12-23 11:11:52 +08:00
 guozozo
现有一个模版,如下:
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
1381 次点击
所在节点    C++
3 条回复
wutiantong
2020-12-23 11:15:27 +08:00
Partial template specialization 偏特化
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
2020-12-23 11:32:21 +08:00
@wutiantong #1 @kirigaya #2 谢谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/738144

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX