c++ - 如何定义依赖于模板参数的 typedef 的 typedef
问题描述
我想制作一个typedef
取决于typedef
模板参数中存在的 a :
struct foo
{
using MyType = int;
};
template <typename T = foo>
struct bar
{
// Pseudo code
#if T::MyType is defined
using MyType = T::MyType;
#else
using MyType = double;
#endif
};
有没有办法让它std::conditional
在 C++14 中使用或其他东西工作?
解决方案
有,有点sfinae。
template<class, typename Fallback, typename = void>
struct type_or_default {
using type = Fallback;
};
template<class C, typename F>
struct type_or_default<C, F, std::void_t<typename C::type>> {
using type = typename C::type;
};
这使用模板元函数公开成员名称的标准约定type
,但您可以根据自己的命名需要对其进行调整。这里唯一的非 C++14 位是std::void_t
,但是可以在 C++14 中实现等效的东西(它只是不能放入 namespace std
)。你在课堂上使用它是这样的:
template <typename T = foo>
struct bar
{
using type = typename type_or_default<T, double>::type;
};
这里发生的是编译器在选择模板特化时进行模式匹配。如果该类C
有一个 member type
,那么我们提供的部分专业化将被认为是更专业化的,因此会被选中。否则(如果在检查特化时替换失败),主模板总是可以回退到的。
修补的现场节目。
推荐阅读
- python - 具有多个查询参数和外键的 DRF 过滤
- javascript - 如何使用 React js 中的上下文将函数从 FUNCTIONAL 传递给 CLASS 组件并在渲染之外(没有 prop)访问它?
- python - 如何将 pandas 数据帧写入具有针对多种数据类型的特定格式的二进制文件?
- django - Django 3.0:无法在模板中获取用户名
- python - Tensorflow 2.1 在 windows 和 linux 上为 Python 3.6 提供了不同的神经网络收敛
- c# - 如何在实体框架的一列中使用具有不同数据的where子句?
- microsoft-edge - 有没有办法将 Microsoft Edge 等 Win 10 内置应用程序复制到另一个窗口?
- python - 如何要求用户安装在python中运行程序所需的要求?
- 6502 - LDA/STA 命令中的 < 或 > 是什么意思?
- python - Python Selenium - 用 YouTube 调试一个简单的代码