我想在某些情况下使用 std::pow
的实现(来自 cmath
),而在其他情况下,我想使用 myspace: :pow
.
namespace myspace
{
template <typename T>
T pow(T val, unsigned exp)
{
if (exp == 0) return 1;
return val*pow(val,exp-1);
}
}
不同的情况由模板参数决定。
template <typename T>
void myFunction()
{
auto val = pow(2.1,3);
/* ... */
}
如果 T == double
,我希望使用 std::pow
计算 val
。如果 T == mydouble
,我希望使用 myspace::pow
计算 val
。现在,我有很多行,例如 auto val = pow(2.1,3);
,我想避免为每一行检查 T
的类型代码。
struct mydouble { /* ... */ };
myFunction<double>(); // uses std::pow
myFunction<mydouble>(); // uses myspace::pow
我一直为此伤脑筋,但找不到解决方案。有什么建议吗?
最佳答案
我可以建议几种解决方案。
类调度器 (C++11)
只需实现一个仿函数,它根据其模板化类型选择正确的实现:
template <typename T>
struct PowerAdapter {
auto operator()(const T& arg, const T& exp) const {
return std::pow(arg, exp);
}
};
template <>
struct PowerAdapter<myspace::MyDouble> {
auto operator()(const myspace::MyDouble& arg, unsigned exp) const {
return myspace::pow(arg, exp);
}
};
您可以按如下方式使用它:
template <typename T>
void myFunction(const T& t) {
using Pow = PowerAdapter<T>;
auto val = Pow{}(t, t);
// ...
}
参数相关查找 (C++98)
如果您的类 MyDouble
与您的 pow
在同一个命名空间中,那么您可以只使用此 C++ 规则:
[...] function names are looked up in the namespaces of their arguments in addition to the scopes and namespaces considered by the usual unqualified name lookup.
所以代码:
template <typename T>
void myFunction(const T& t) {
pow(t, 12);
}
会根据T
的命名空间选择合适的pow
。
请注意,在 double
的情况下,您要从 global namespace 的 math.h
中选择 pow
(不合格的名称查找)。
我个人不喜欢这种方法,因为它隐藏了选择机制并且更难扩展。
if constexpr
(C++17)
您可以在编译时选择一个合适的分支。 将您的选择逻辑包装到适当的函数(或仿函数)中。像这样的东西:
template <typename T, typename U>
auto PowerAdapter(const T& val, const U& exp) {
if constexpr (std::is_same_v<T, myspace::MyDouble>) {
return myspace::pow(val, exp);
} else {
return std::pow(val, exp);
}
}
关于c++ - 在模板参数的函数中使用一个或另一个命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57411828/