标签: compiler-bug

Constexpr如果是非bool条件

我似乎找到了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,但我不完全确定.如果这是任何一个编译器的错误,是否已报告?

c++ language-lawyer compiler-bug implicit-conversion c++17

41
推荐指数
2
解决办法
1688
查看次数

条件运算符的返回类型和两阶段查找

请考虑以下代码段:

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&)被选中.

哪个编译器是对的?

c++ dependent-name language-lawyer compiler-bug name-lookup

39
推荐指数
2
解决办法
1085
查看次数

在C#中转换为'int'时,'const float'值与'float'不同

你们任何人都可以解释为什么会这样吗?

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)

.net c# compiler-bug

37
推荐指数
1
解决办法
2391
查看次数

为什么gcc和clang各自为这个程序产生不同的输出?(转换运算符与构造函数)

程序:

#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)

c++ gcc clang language-lawyer compiler-bug

34
推荐指数
1
解决办法
4146
查看次数

VBA 在 If 语句中采用错误的分支 - 严重的编译器错误?

标题中的问号只是因为我非常不愿意将任何东西称为编译器错误,但在这种情况下,如果有人能以任何其他方式解释这种行为,我会感到惊讶。

重现问题的代码非常非常简单。在标准模块中,我们有以下内容:

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)

oop excel vba if-statement compiler-bug

34
推荐指数
1
解决办法
1087
查看次数

Visual Studio 2012不同的值释放/调试模式

在调试和发布模式之间切换时,此代码在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

33
推荐指数
3
解决办法
1869
查看次数

仅使用 std::function 和 std::shared_pointer 在 C++ 标准库中进行双重释放

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)

c++ g++ libstdc++ undefined-behavior compiler-bug

32
推荐指数
1
解决办法
1659
查看次数

Visual C++ 2012(x86)中可能的编译器错误?

我正在使用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

29
推荐指数
1
解决办法
1933
查看次数

可能是Visual Studio 2015中的C#编译器错误

我认为这是一个编译器错误.

使用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) …

c# compiler-bug roslyn visual-studio-2015 coreclr

28
推荐指数
2
解决办法
1535
查看次数

(已知)VC12中的编译器错误?

当使用VC12(在Visual Studio 2013 RTM中)[1]编译时,此程序会导致崩溃(在所有构建配置中),而实际上它不应该:

#include <string>

void foo(std::string const& oops = {})
{
}

int main()
{
    foo();
}
Run Code Online (Sandbox Code Playgroud)

我知道两个可能有关的无声的错误代码错误:

老实说,我认为这些是不同的.有人知道吗

  1. 是否存在关于连接的主动跟踪错误
  2. 是否有解决方法(或导致此错误的情况的明确描述,所以我们可以在我们的代码库中查找/避免它)?

[1]使用C++ Console Application'向导'创建一个空项目.为简单起见,请禁用预编译标头并保留所有默认值:http://i.stack.imgur.com/rrrnV.png

c++ visual-c++ compiler-bug visual-studio-2013

26
推荐指数
2
解决办法
2961
查看次数