我似乎找到了Clang和GCC不同意的东西.这是代码:
int main() {
if constexpr (2) {}
}
Run Code Online (Sandbox Code Playgroud)
这成功编译了GCC 7.4.0,但它与Clang 7.0.0失败并出现此错误消息:
test.cpp:3:17: error: constexpr if condition evaluates to 2, which cannot be narrowed to type 'bool'
[-Wc++11-narrowing]
if constexpr (2) {}
^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
cppreference似乎没有提到"缩小",所以这看起来像一个Clang bug,但我不完全确定.如果这是任何一个编译器的错误,是否已报告?
请考虑以下代码段:
struct Base { };
struct Derived : Base { };
void f(Base &) { std::cout << "f(Base&)\n"; }
template <class T = int>
void g() {
Derived d;
f(T{} ? d : d); // 1
}
void f(Derived &) { std::cout << "f(Derived&)\n"; }
int main() {
g();
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我认为应该在第一阶段查找对fat 的函数调用// 1,因为它的参数的类型是明确的Derived&,因此可以解析为f(Base&)范围中唯一的一个.
Clang 3.8.0同意我的观点,但是GCC 6.1.0没有,并推迟f到第二阶段的查找,在那里f(Derived&)被选中.
哪个编译器是对的?
你们任何人都可以解释为什么会这样吗?
static void Main()
{
const float xScaleStart = 0.5f;
const float xScaleStop = 4.0f;
const float xScaleInterval = 0.1f;
const float xScaleAmplitude = xScaleStop - xScaleStart;
const float xScaleSizeC = xScaleAmplitude / xScaleInterval;
float xScaleSize = xScaleAmplitude / xScaleInterval;
Console.WriteLine(">const float {0}, (int){1}", xScaleSizeC, (int)xScaleSizeC);
Console.WriteLine("> float {0}, (int){1}", xScaleSize, (int)xScaleSize);
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
输出:
>const float 35, (int)34
> float 35, (int)35
Run Code Online (Sandbox Code Playgroud)
我知道0.1的二进制表示实际上是0.09999990463256835937,虽然为什么这会发生'const float'而不是'float'?这被认为是编译器错误吗?
为了记录,代码编译成:
private static void Main(string[] args)
{
float xScaleSize = 35f;
Console.WriteLine(">const float {0}, (int){1}", …Run Code Online (Sandbox Code Playgroud) 程序:
#include <stdio.h>
struct bar_t {
int value;
template<typename T>
bar_t (const T& t) : value { t } {}
// edit: You can uncomment these if your compiler supports
// guaranteed copy elision (c++17). Either way, it
// doesn't affect the output.
// bar_t () = delete;
// bar_t (bar_t&&) = delete;
// bar_t (const bar_t&) = delete;
// bar_t& operator = (bar_t&&) = delete;
// bar_t& operator = (const bar_t&) = delete;
};
struct foo_t {
operator int …Run Code Online (Sandbox Code Playgroud) 标题中的问号只是因为我非常不愿意将任何东西称为编译器错误,但在这种情况下,如果有人能以任何其他方式解释这种行为,我会感到惊讶。
重现问题的代码非常非常简单。在标准模块中,我们有以下内容:
Sub CompilerBug()
Dim oClass As cClass
Set oClass = New cClass
If False Then
Debug.Print "This doesn't print, as it shouldn't."
End If
If Falsee(oClass.Clone) Then
Debug.Print "This does print, although it shouldn't!"
End If
End Sub
Public Function Falsee(oClass As cClass) As Boolean
Falsee = False
End Function
Run Code Online (Sandbox Code Playgroud)
我们cClass在名为 cClass 的类模块中定义了一个类 ( ),包含以下代码:
Public Function Clone() As cClass
Dim oClass As cClass
Set oClass = New cClass
Set Clone = oClass
End Function
Private Sub …Run Code Online (Sandbox Code Playgroud) 在调试和发布模式之间切换时,此代码在MSVS 2012,Windows 7中生成不同的值:
#include <iostream>
using namespace std;
int A[20000];
int main() {
int shift = 0;
int Period = 30;
//Fill array
for(int i = 0; i < 20000; i++) {
A[i] = i * 2 + 123;
}
int sumTotal = 0;
int sum = 0;
for(int bars = Period + 10; bars < 1000; bars++) {
sum = 0;
for(int i = 0; i< Period; i++) {
sum += A[bars - i];
}
sumTotal += sum;
} …Run Code Online (Sandbox Code Playgroud) c++ visual-c++ compiler-bug visual-studio-2012 visual-studio-2013
shared_ptr我最近在 lambda 中捕获 a 时在程序中遇到了一个奇怪的双重释放错误。我能够将其减少为以下最小示例:
#include <memory>
#include <functional>
struct foo {
std::function<void(void)> fun;
};
foo& get() {
auto f = std::make_shared<foo>();
// Create a circular reference by capturing the shared pointer by value
f->fun = [f]() {};
return *f;
}
int main(void) {
get().fun = nullptr;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用 GCC 12.2.0 和地址清理程序编译并运行它,会在 中产生双重释放std::function:
$ g++ -fsanitize=address -g -Wall -Wextra -o main main.cpp && ./main
=================================================================
==2401674==ERROR: AddressSanitizer: attempting double-free on 0x602000000010 in thread T0:
#0 …Run Code Online (Sandbox Code Playgroud) 我正在使用VC++ 11(CTP Update 1)编译x86目标时遇到随机浮点错误.请参阅下面的简短示例"test.cpp",并使用以下命令进行编译:
cl /GL /O2 /EHsc test.cpp /link /MACHINE:X86
Run Code Online (Sandbox Code Playgroud)
输出应该是10 == 10,但它会10 == 0在/GL(整个程序优化)启用时产生.问题似乎是get_scaling_factor()将结果推送到浮点堆栈,但调用函数期望它在SSE寄存器XMM0中.
问题:我错过了一些明显的东西,还是这真的是一个错误?当然,测试程序没有意义,因为它是一个简化的测试用例.
TEST.CPP:
#include <iostream>
template <typename T>
inline T get_scaling_factor(int units)
{
switch (units)
{
case 0: return 1;
case 1: return 10;
case 2: return 100;
case 3: return 1000;
case 4: return 10000;
case 5: return 100000;
case 6: return 1000000;
case 7: return 10000000;
case 8: return 100000000; …Run Code Online (Sandbox Code Playgroud) c++ visual-c++ compiler-bug visual-studio-2012 visual-c++-2012
我认为这是一个编译器错误.
使用VS 2015编译时,以下控制台应用程序编译并执行完美:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var x = MyStruct.Empty;
}
public struct MyStruct
{
public static readonly MyStruct Empty = new MyStruct();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是现在它变得很奇怪了:这个代码编译,但它会TypeLoadException在执行时抛出.
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var x = MyStruct.Empty;
}
public struct MyStruct
{
public static readonly MyStruct? Empty = null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
你遇到同样的问题吗?如果是这样,我将在Microsoft提出问题.
代码看起来毫无意义,但我用它来提高可读性并实现消歧.
我有不同的重载方法,如
void DoSomething(MyStruct? arg1, string arg2)
void DoSomething(string arg1, string arg2) …
当使用VC12(在Visual Studio 2013 RTM中)[1]编译时,此程序会导致崩溃(在所有构建配置中),而实际上它不应该:
#include <string>
void foo(std::string const& oops = {})
{
}
int main()
{
foo();
}
Run Code Online (Sandbox Code Playgroud)
我知道两个可能有关的无声的错误代码错误:
老实说,我认为这些是不同的.有人知道吗
[1]使用C++ Console Application'向导'创建一个空项目.为简单起见,请禁用预编译标头并保留所有默认值:http://i.stack.imgur.com/rrrnV.png
compiler-bug ×10
c++ ×7
visual-c++ ×3
c# ×2
.net ×1
c++17 ×1
clang ×1
coreclr ×1
excel ×1
g++ ×1
gcc ×1
if-statement ×1
libstdc++ ×1
name-lookup ×1
oop ×1
roslyn ×1
vba ×1