This is about as simplified as I could make a toy example that still hit the bug:
struct Vector3f64 {
double x;
double y;
double z;
};
struct Vector3f32 {
float x;
float y;
float z;
};
// I use this to select their element type in functions:
template <typename T>
using param_vector = std::conditional_t<std::is_same_v<std::remove_const_t<std::remove_reference_t<T>>, Vector3f64>, double, float>;
// This is the function I want to pull the return type from:
template <typename T>
T VectorVolume(const T x, const T y, const T z) {
return x * x + y * y + z * z;
}
template<class R, class... ARGS>
std::function<R(ARGS...)> make_func(R(*ptr)(ARGS...)) {
return std::function<R(ARGS...)>(ptr);
}
// This function fails to compile:
template <typename T>
typename decltype(make_func(&VectorVolume<param_vector<T>>))::result_type func(const T& dir) {
return VectorVolume(dir.x, dir.y, dir.z);
}
int main() {
const Vector3f64 foo{ 10.0, 10.0, 10.0 };
std::cout << func(foo) << std::endl;
}
The make_func
is from SergyA's answer which I wanted to create a std::function
so I could find a return type without explicitly declaring the parameters VectorVolume
took. But I get this error from visual-studio-2017 version 15.6.7:
error C2039:
result_type
: is not a member of 'global namespace' error C2061: syntax error: identifierfunc
error C2143: syntax error: missing;
before{
error C2447:{
: missing function header (old-style formal list?) error C3861:func
: identifier not found
This works fine on c++14 in g++: https://ideone.com/PU3oBV It'll even work fine on visual-studio-2017 if I don't pass the using
statement as a template parameter:
template <typename T>
typename decltype(make_func(&VectorVolume<double>))::result_type func(const T& dir) {
return VectorVolume(dir.x, dir.y, dir.z);
}
This is almost identical to the problem I worked around here: Templated usings Can't be Nested in Visual Studio Unfortunately, in that case I could just replace my function construction with a result_of
call. In this case I just don't see how I can redesign make_func
to work around this bug. Does anyone know of a workaround? (Other than upgrading to 15.9.5, which does solve this.)
You're really only interested in the function::result_type
so there's really no need to go through the bugged path of returning a function
. Just return the result type and do a decltype on that (you don't even need to define the function since you're not actually calling it.) Something like this:
template <typename R, typename... ARGS>
R make_func(R(*)(ARGS...));
Then just directly use the return type:
template <typename T>
decltype(make_func(&VectorVolume<param_vector<T>>)) func(const T& dir) {
return VectorVolume(dir.x, dir.y, dir.z);
}
This is works great on Visual Studio 15.6.7 and as an added bonus is fully c++14 compatible: https://ideone.com/gcYo8x