模板化变量,编译器bug? (msvc 19.00.23918 x86) [英] Templated variables, compiler bug? (msvc 19.00.23918 x86)

查看:68
本文介绍了模板化变量,编译器bug? (msvc 19.00.23918 x86)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下编译并运行(msvc 19.00.23918 x86)但打印0. 它应该打印3.14。 这是编译器错误吗?

The following compiles and runs (msvc 19.00.23918 x86) but prints 0.  It should print 3.14.  Is this a compiler bug?

#include "stdafx.h"

#include <iostream>
#include <vector>

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

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

注意用常量替换auto替换成功:

Note that replacing auto with T for the constant succeeds:

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

推荐答案

这是一个有趣的问题,因为改变T的auto成功。

That is an interesting question, since changing the auto for T succeeds.

可能是这样,所以我建议你测试一下,看看是否会出现最新的Visual Studio版本(Visual Studio 2017 15.5或15.6预览版)如果是这样,那么使用Visual Studio本身的反馈工具向Microsoft报告此错误。
它位于Visual Studio主窗口的右上角。

It probably is though, so I would suggest you test to see if this occurs with the latest Visual Studio release (Visual Studio 2017 15.5 or 15.6 preview) and if it does, then report this as a bug to Microsoft using the feedback tool in Visual Studio itself. It is in the top right of the Visual Studio main window.

有趣的是,如果你在main中使用相同的cout,那么它将打印正确的pi值。它似乎是变量模板和lambda函数的组合导致了这个问题。

Interestingly, if you use the same cout in main itself then it will print the correct value of pi. It seems to be the combination of variable templates and lambda functions which causes the problem.


这篇关于模板化变量,编译器bug? (msvc 19.00.23918 x86)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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