首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模板化使用不能嵌套在Visual Studio中

模板化使用不能嵌套在Visual Studio中
EN

Stack Overflow用户
提问于 2019-03-02 00:55:57
回答 1查看 105关注 0票数 0

这几乎是我所能做的最简单的玩具示例,仍然可以找到bug:

代码语言:javascript
复制
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来自Guillaume Racicot's answer,我想用它来查找返回类型。但是我从visual-studio-2017版本15.6.7得到了这个错误:

代码语言:javascript
复制
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上运行得很好,如果我不将一条using语句传递给另一条语句,它甚至可以在visual-studio-2017上运行得很好:

代码语言:javascript
复制
template <typename T>
call_t<decltype(&VectorVolume<param_vector<T>>), double> func(const T& param) {
    return VectorVolume(param.x, param.y, param.z);
}

有什么办法可以解决这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-02 02:38:29

As mentioned by @NathanOliver正确的解决方案是升级到15.9.5,修复此问题。但除非您可以在15.6.7上使用result_of or invoke_result解决此问题,方法是将call_t更改为:

代码语言:javascript
复制
template<typename F, typename T>
using call_t = result_of_t<F&&(T, T, T)>;

请注意,在c++17result_of已被弃用,因此如果您使用"/std:c++17“或"/std:c++latest”运行,这将不起作用,您需要使用更方便的:

代码语言:javascript
复制
template<typename F, typename T>
using call_t = invoke_result_t<F, T, T, T>;

值得注意的是,Guillaume Racicot's answer使用了一种优雅的veradic模板,如果您将func的定义更改为:

代码语言:javascript
复制
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);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54949124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档