变量模板中的除法在Visual Studio 2017中返回零 [英] Division in Variable Template Returns Zero in Visual Studio 2017

查看:111
本文介绍了变量模板中的除法在Visual Studio 2017中返回零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是 visual-studio-2017 与以下问题相关的错误: Visual Studio中Lambda的模板化变量错误? 评论中提到的似乎与优化程序有关。

This is probably a visual-studio-2017 bug related to this question: Templated Variables Bug With Lambdas in Visual Studio? And as mentioned in the comments there seems to be optimizer related.

变量模板在Visual Studio 2017中似乎有一个错误。因此,此代码例如:

Division in the definition of a variable template seems to have a bug in Visual Studio 2017. So this code for example:

template <typename T>
const T PI = std::acos(static_cast<T>(-1));

template <typename T>
const T ONE_EIGHTY = 180;

template <typename T>
const T DEG_TO_RAD = PI<T> / ONE_EIGHTY<T>;

int main() {
    cout << DEG_TO_RAD<float> << endl;
}

在gcc 6.3 上将输出:


0.0174533

0.0174533

在Visual Studio 2017上,此输出:

On Visual Studio 2017 this outputs:


0.0

0.0

我假设这是另一个Visual Studio错误?

I'm assuming this is another Visual Studio bug? Is there a workaround here?

推荐答案

应@JonathanMee的要求在此处发布解决方法,因为它也可以解决,因为他之前曾报道过类似的问题。似乎与最新的VS2017中的某种错误有关,该错误阻止模板自动激活并需要强制激活:

Posting a workaround here at the request of @JonathanMee as it also works for the similar problem reported by him earlier. Seems to be connected with some sort of bug in latest VS2017 that prevents template from activating automatically and needs a force activation:

template <typename T>
const T PI = std::acos(static_cast<T>(-1));

template <typename T>
const T ONE_EIGHTY = 180;

template <typename T>
const T DEG_TO_RAD = PI<T> / ONE_EIGHTY<T>;

int main() 
{
    PI<float>; // <---- workaround
    std::cout << DEG_TO_RAD<float> << std::endl;
}

以下是向Microsoft提交的故障单: https://developercommunity.visualstudio.com/content /problem/207741/template-needs-be-be-force-instantiated-vs2017-bug.html

Here is a bug ticket filed with Microsoft: https://developercommunity.visualstudio.com/content/problem/207741/template-needs-to-be-force-instantiated-vs2017-bug.html

这篇关于变量模板中的除法在Visual Studio 2017中返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆