C++11 constexpr causes compiler's internal err

2019-06-24 06:59发布

I am using Visual Studio 2015 Update 3.

I get a fatal error:

(code C1001) : An internal error has occurred in the compiler.

Here is the code :

template<typename T>
constexpr T epsilon = std::numeric_limits<T>::epsilon();

I read it was fixed in Visual Studio Update 2. Can someone explain me why I am getting this error? Thanks in advance.

2条回答
姐就是有狂的资本
2楼-- · 2019-06-24 07:35

I've raised this as a bug with Microsoft, but they have had a fix since early 2017 which has not been released from what I can see as of today.

I've also provided a project on GitLab and given information to Microsoft for this project here: https://gitlab.com/cppocl/tostring

Loading the .sln and compiling currently crashes with Visual Studio 2015 update 2 or 3, and Visual Studio Enterprise 2017 version 15.3.1.

It does seem that the combination of template and constexpr causes the compiler to crash.

I've seen reports for Visual Studio 2017 describing similar types of problems.

This link says fixed pending release: https://developercommunity.visualstudio.com/content/problem/18155/msvc-2017-c-fatal-error-c1001-constexpr-initializa.html

Visual Studio 2015 backlog of bugs relating to constexpr is here: https://blogs.msdn.microsoft.com/vcblog/2015/12/02/constexpr-in-vs2015-update-1/

EDIT: I also don't believe changing optimization settings will make any difference, as has been recommended in other posts. I have experimented with these settings and applied recommended patches without success so far.

查看更多
三岁会撩人
3楼-- · 2019-06-24 07:42

Any internal error (ICE) is a compiler bug. You get it because you have happened to trigger that bug. For this compiler you can report it at Microsoft Connect.

For such a report you need an example with an expected correct result, and the erroneous result.

The following test program compiles & runs nicely with MinGW g++ 5.1

#include <limits>

template<typename T>
constexpr T epsilon = std::numeric_limits<T>::epsilon();

#include <iostream>
using namespace std;
auto main() -> int
{
    cout << epsilon<double> << endl;
}

Output:

2.22045e-016

With Visual C++ 2015 update 2 it produces an ICE:

foo.cpp(10): fatal error C1001: An internal error has occurred in the compiler.
(compiler file 'f:\dd\vctools\compiler\cxxfe\sl\p1\c\symbols.c', line 28114)
 To work around this problem, try simplifying or changing the program near the locations listed above.
Please choose the Technical Support command on the Visual C++
 Help menu, or open the Technical Support help file for more information
foo.cpp(10): note: see reference to variable template 'const double epsilon' being compiled

Compiler version:

> cl /nologo- 2>&1 | find "++"
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23725 for x86
查看更多
登录 后发表回答