c++ - Visual Studio 中 Lambda 的模板化变量错误?

标签 c++ lambda visual-studio-2017 c++14 variable-templates

提供 variable templates 中哪个工作正常,但在 lambda 中,它们似乎分崩离析。例如:

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这输出:

3.14159

在 Visual Studio 2017 上输出:

0.0

最佳答案

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

关于c++ - Visual Studio 中 Lambda 的模板化变量错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49117293/

相关文章:

c++ - 如何尽快实现strlen

c++ - 为什么我的 removestring 函数有错误

android - 部署失败 - 设备上没有剩余空间

c# - Visual Studio 2017 扩展开发

c++ - 如何不使用 `export` 关键字从模块导出函数和类?

c++ - 如何在 C++ 中将字符串转换为 char *?

c++ - libmediainfo 有替代方案吗?

c# - T 未知时如何创建 Func<T, bool>

c# - IQueryable 不包含 'Select' 的定义

c++ - 强制将对象作为拷贝传递给接收通用引用的函数