我在上游库中有一个要专门研究的模板化函数:
/// 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/