c++14提供 variable templates在 visual-studio-2017 中哪个工作正常,但在 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/