c++ - 如何使用模板参数包实现 SFINAE 仅限于少数类型
问题描述
我需要启用 std::string 和 int,但使用参数包。
template <typename... ParamType, typename = typename std::enable_if<std::is_same<ParamType..., std::string>::value || std::is_same<ParamType..., int>::value>::type>
static inline void Log(const ParamType & ... args)
{
}
但我打电话时出错
Log("hello"s, "world"s); //syntax ERROR
期望的结果
Log(4,3,"Hello"s); //OK
Log("Hello"s); //OK
Log(5); //OK
解决方案
该解决方案使用了一些 C++17-ism ( std::void_t
)。如果需要,它们的各种实现适用于随处可见的早期 C++ 标准:
template <typename ... ParamType,
typename = std::void_t<std::enable_if_t
<std::is_same_v<ParamType, std::string> ||
std::is_same_v<ParamType, int>>...>>
static inline void Log(const ParamType & ... args)
{
}
推荐阅读
- java - 杀死应用程序时,SharedPreferences 消失
- python - 有人可以解释这些行:X1, y1 = np.c_[np.random.normal(loc=new_center[0],
- page-tables - 页表在哪里?
- xpath - 如何使用 descendant-or-self:: 前缀在 XPath 中获取直接子级?
- c# - ASP.NET CORE 2.2 使用 dotConnect for MySQL 作为提供程序和实体框架扩展:无法解析提供程序
- mysql - 应该使用哪个查询?从 MySQL Explain 推导出来
- c# - 使用 ExifTool 覆盖现有标签
- c# - Firebase REST API - 使用 id 令牌验证请求不起作用
- terraform - 将变量添加到数据
- javascript - 根据下拉字段的值选择对象的值