Guillaume Racicot gave an excellent answer to this question on how I could specialize template variables. But I'm having trouble in visual-studio-2017 with creating a templated array of function pointers. This code for example:
struct vec
{
double x;
double y;
double z;
};
namespace details
{
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 double (*my_temp[])(const vec&) = { &details::X<T>, &details::Y<T> };
template <typename T>
constexpr double (*my_temp<T, enable_if_t<is_floating_point_v<decltype(details::X(T()))>>>[])(const vec&) = { &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;
}
}
1
2
3
But in visual-studio-2017 only outputs:
1
2
Is there something I can do to work around this?
/std:c++17
, did you maybe mean Visual Studio 2019? – Interlocutory/std:c++14
nor/std:c++17
. So, are you really using Visual Studio 2017? – Interlocutorystd::array
, it works on all three compilers: godbolt.org/z/Ro-WHZ – Trevethickarray
is the workaround then? I'll try locally... – Dauphinarray
works locally. Seems like the most viable workaround available to us at this point if you care to post an answer? – Dauphin