【发布时间】:2019-11-02 04:53:48
【问题描述】:
this saga 中还有一个问题。 Guillaume Racicot 足以为我提供yet another workaround 所以这是我将基于此问题的代码:
struct vec
{
double x;
double y;
double z;
};
namespace details
{
template <typename T>
using subscript_function = double(*)(const T&);
template <typename T>
constexpr double X(const T& param) { return param.x; }
template <typename T>
constexpr double Y(const T& param) { return param.y; }
template <typename T>
constexpr double Z(const T& param) { return param.z; }
}
template <typename T, typename = void>
constexpr details::subscript_function<T> my_temp[] = { &details::X<T>, &details::Y<T> };
template <typename T>
constexpr details::subscript_function<T> my_temp<T, enable_if_t<is_floating_point_v<decltype(details::X(T()))>, T>>[] = { &details::X<T>, &details::Y<T>, &details::Z<T> };
int main() {
vec foo = { 1.0, 2.0, 3.0 };
for(const auto i : my_temp<decltype(foo)>) {
cout << (*i)(foo) << endl;
}
}
当我返回void 以外的other 内容时,问题似乎出现在我的专业领域。例如,在上面的代码中,enable_if_t<is_floating_point_v<decltype(details::X(T()))>, T> 防止特化,而简单地删除最后一个参数并允许 enable_if 返回 void 允许特化。
我认为这表明我对这里真正发生的事情的误解。为什么专用类型必须始终为 void 才能正常工作?
【问题讨论】:
标签: c++ templates template-specialization enable-if template-variables