Templated Variables Bug With Lambdas in Visual Studio?

天涯浪子 提交于 2019-12-10 12:48:21

问题


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();
}

On gcc 6.3 this outputs:

3.14159

On Visual Studio 2017 this outputs:

0.0


回答1:


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 for example: https://ideone.com/9UdwBT



来源:https://stackoverflow.com/questions/49117293/templated-variables-bug-with-lambdas-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!