c++ - 禁用函数模板特化或使用constexpr变量重载

标签 c++ template-specialization enable-if glog

我在上游库中有一个要专门研究的模板化函数:

/// glog/logging.h
namespace google {
  template <typename T>
  inline void MakeCheckOpValueString(std::ostream* os, const T& v) {
    (*os) << v;
  }
} // namespace google
我是通过在代码中定义来实现的
namespace google {
  template <>
  inline void MakeCheckOpValueString<std::chrono::nanoseconds>(std::ostream* os, const Duration& d) {
    (*os) << v.cound();
  }
} // namespace google
我想在编译时(即不通过预处理器宏)使用类似于
constexpr bool provide_my_specializations = ...;

template <std::enable_if<provide_my_specializations>>
  inline void MakeCheckOpValueString<std::chrono::nanoseconds>(std::ostream* os, const Duration& d)
但是我没有找到将enable_if放入模板特化的方法。是否可以使用sfinae 启用/禁用模板特化,而无需修改常规模板?
编辑:
给定评论,我尝试使用函数重载,尽管未能找到正确的位置来禁用sfinae
constexpr bool SWITCH = false;

namespace glog {
inline std::enable_if_t<SWITCH, void> MakeCheckOpValueString(std::ostream* os, const Duration& v) {
  (*os) << v.count();
}
}

compiler-explorer link
编辑2:
我没有尝试使用我要专门研究的功能,而是在上游库中使用它,因此我不能调用其他(包装)对象,因为这需要在上游编辑调用站点。
(事实证明,这使得重载而不是特化变得棘手-尽管并非不可能
// glog/logging.h
namespace google {
template <typename T>
inline void MakeCheckOpValueString(std::ostream* os, const T& v) {
  (*os) << v;
}
tempate <typename T1, typename T2>
std::string* MakeCheckOpString(const T1& v1, const T2& v2, const char* exprtext) {
  base::CheckOpMessageBuilder comb(exprtext);
  MakeCheckOpValueString(comb.ForVar1(), v1);
  MakeCheckOpValueString(comb.ForVar2(), v2);
  return comb.NewString();
}

// my code
// non-templated overload of MakeCheckOpValueString needs to go here
#include <glog/logging.h>
// template specialization of MakeCheckOpValueString can go here

/*
code that eventually instantiates MakeCheckOpString comes here
*/
)

最佳答案

std::enable_if_t<false>无效(对于任何特化)。
为了使其对SFINAE友好,您必须使条件依赖:

constexpr bool SWITCH = false;

namespace glog {

template <bool b = SWITCH>
std::enable_if_t<b> MakeCheckOpValueString(std::ostream* os, const Duration& v) {
  (*os) << v.count();
}
}

关于c++ - 禁用函数模板特化或使用constexpr变量重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64243813/

相关文章:

android - 自动重启Linux控制台应用程序

c++ - 嵌套模板特化取决于封闭的模板参数

c++ - 强制非专用模板实例化出现编译错误

c++ - enable_if 如何帮助选择类模板的特化?

c++ - 我可以为不同的类型集定义模板吗?

c++ - 如何通过网络传递 cv::Mat?

c++ - Visual Studio 需要为每次更改重建解决方案

c++ - 根据可散列的模板使用正确的容器类型

c++ - SFINAE 用作返回类型,但不是参数类型

c++ - 如何使用 Bison (Yacc) 在 C++ 中生成 64 位解析器?