在Visual Studio中使用Lambda进行模板化变量错误吗? [英] Templated Variables Bug With Lambdas in Visual Studio?

查看:74
本文介绍了在Visual Studio中使用Lambda进行模板化变量错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c ++ 14 提供变量模板,但在lambda中,似乎分崩离析。例如:

c++14 provides variable templates Which work fine in visual-studio-2017, but within lambdas they seem to fall apart. For example:

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

int main() {
  auto func = []() { cout << PI<float> << endl; };

  func();
}

在gcc 6.3 上将输出:


3.14159

3.14159

在Visual Studio 2017上,此输出为:

On Visual Studio 2017 this outputs:


0.0

0.0


推荐答案

似乎有一个可靠的解决方法,它既适用于这种情况,也适用于相关案例。在这两种情况下,强制激活模板似乎都能在VS2017中完成:

Wierd bug, but it seems to have a reliable workaround, which works for both, this case and the related case. In both cases forcefully activating template seems to do the job in VS2017:

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

int main() 
{
  PI<float>; // <------ this
  auto func = []() { std::cout << PI<float> << std::endl; };

  func();
}

GCC 6.3例如: https://ideone.com/9UdwBT

GCC 6.3 for example: https://ideone.com/9UdwBT

这篇关于在Visual Studio中使用Lambda进行模板化变量错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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