9

提供了变量模板,这些模板在中工作正常,但在 lambdas 中它们似乎分崩离析。例如:

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

在 Visual Studio 2017 上,此输出:

0.0

4

1 回答 1

3

奇怪的错误,但它似乎有一个可靠的解决方法,适用于这种情况和相关情况。在这两种情况下,强制激活模板似乎都可以在 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

于 2018-03-05T21:02:51.963 回答