模板化用法不能嵌套在 Visual Studio
Templated usings Can't be Nested in Visual Studio
这已经尽可能简化了,但仍然存在错误:
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<typename F, typename T>
using call_t = decltype(std::declval<F>()(std::declval<T>(), std::declval<T>(), std::declval<T>()));
// This function fails to compile:
template <typename T>
call_t<decltype(&VectorVolume<param_vector<T>>), param_vector<T>> func(const T& param) {
return VectorVolume(param.x, param.y, param.z);
}
int main() {
const Vector3f64 foo{ 10.0, 10.0, 10.0 };
std::cout << func(foo) << std::endl;
}
call_t
来自 which I wanted to use to find a return type. But I get this error from visual-studio-2017 版本 15.6.7:
error C2064: term does not evaluate to a function taking 3 arguments<br>
note: see reference to alias template instantiation 'call_t<unknown-type,double>' being compiled
note: see reference to function template instantiation 'unknown-type func(const T &)' being compiled
这在 g++ 上工作正常:https://coliru.stacked-crooked.com/a/48b18b66c39486ef It'll even work fine on visual-studio-2017 如果我不将一个 using
语句传递给另一个:
template <typename T>
call_t<decltype(&VectorVolume<param_vector<T>>), double> func(const T& param) {
return VectorVolume(param.x, param.y, param.z);
}
我有办法解决这个问题吗?
the right solution is to upgrade to 15.9.5 where this is fixed. But barring that you can use result_of
or invoke_result
通过将 call_t
更改为:
来解决 15.6.7 上的问题
template<typename F, typename T>
using call_t = result_of_t<F&&(T, T, T)>;
请注意,result_of
在 c++17 中已弃用,因此如果您是 运行 "/std:c++17" 或 "/std:c++latest" 这将获胜不行,你需要使用更方便的:
template<typename F, typename T>
using call_t = invoke_result_t<F, T, T, T>;
值得注意的是 使用了一个优雅的 veradic 模板,如果您将 func
的定义更改为:
template <typename T>
call_t<decltype(&VectorVolume<param_vector<T>>), param_vector<T>, param_vector<T>, param_vector<T>> func(const T& param) {
return VectorVolume(param.x, param.y, param.z);
}
这已经尽可能简化了,但仍然存在错误:
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<typename F, typename T>
using call_t = decltype(std::declval<F>()(std::declval<T>(), std::declval<T>(), std::declval<T>()));
// This function fails to compile:
template <typename T>
call_t<decltype(&VectorVolume<param_vector<T>>), param_vector<T>> func(const T& param) {
return VectorVolume(param.x, param.y, param.z);
}
int main() {
const Vector3f64 foo{ 10.0, 10.0, 10.0 };
std::cout << func(foo) << std::endl;
}
call_t
来自
error C2064: term does not evaluate to a function taking 3 arguments<br>
note: see reference to alias template instantiation 'call_t<unknown-type,double>' being compiled
note: see reference to function template instantiation 'unknown-type func(const T &)' being compiled
这在 g++ 上工作正常:https://coliru.stacked-crooked.com/a/48b18b66c39486ef It'll even work fine on visual-studio-2017 如果我不将一个 using
语句传递给另一个:
template <typename T>
call_t<decltype(&VectorVolume<param_vector<T>>), double> func(const T& param) {
return VectorVolume(param.x, param.y, param.z);
}
我有办法解决这个问题吗?
result_of
or invoke_result
通过将 call_t
更改为:
template<typename F, typename T>
using call_t = result_of_t<F&&(T, T, T)>;
请注意,result_of
在 c++17 中已弃用,因此如果您是 运行 "/std:c++17" 或 "/std:c++latest" 这将获胜不行,你需要使用更方便的:
template<typename F, typename T>
using call_t = invoke_result_t<F, T, T, T>;
值得注意的是 func
的定义更改为:
template <typename T>
call_t<decltype(&VectorVolume<param_vector<T>>), param_vector<T>, param_vector<T>, param_vector<T>> func(const T& param) {
return VectorVolume(param.x, param.y, param.z);
}