标签: compiler-bug

为什么编译?代码似乎打破了类型参数的约束

在下面的测试中,TesterClass对其两个类型参数之间的关系设置了约束.方法func2()似乎打破了这个约束,我希望它会在某处导致类型编译错误(在func2的定义中,或者每当类与String之外的任何第二个参数一起使用时),但它不会!

此外,如果我调用func2并将结果保存在适当类型的变量中,则编译失败(在该变量的类型上).但是做同样的事情并保存在更通用的类型(例如Object)中会成功,尽管事实上函数的返回类型在两种情况下都应该具有相同的类型(在向上转换之前).

这里发生了什么?

谢谢!

public class TestGenerics {
    public static class ParamedType<T> {}


    public class TesterClass<A extends ParamedType<B>, B> {
        public TesterClass<A, B> func() {
            return new TesterClass<A, B>();
        }

        public TesterClass<A, String> func2() {
            return new TesterClass<A, String>();
        }
    }

    public Object test() {
        // How can I use these type parameters? Doesn't .func2 now have an invalid return type?
        TesterClass<ParamedType<Integer>,Integer> testClass = new TesterClass<TestGenerics.ParamedType<Integer>, Integer>();

        //TesterClass<ParamedType<String>, Integer> res2 = testClass.func2(); // <-- will not compile
        Object res = …
Run Code Online (Sandbox Code Playgroud)

java eclipse generics compiler-bug eclipse-jdt

4
推荐指数
1
解决办法
411
查看次数

令人高兴地连接不兼容的类型会导致混乱

我一直试图找出一些边界g++,特别是链接(C++)目标文件.我发现以下好奇心,我试着尽可能多地压缩,然后再询问.

文件 common.h

#ifndef _COMMON_H
#define _COMMON_H

#include <iostream>

#define TMPL_Y(name,T) \
struct Y { \
  T y; \
  void f() { \
    std::cout << name << "::f " << y << std::endl; \
  } \
  virtual void vf() { \
    std::cout << name << "::vf " << y << std::endl; \
  } \
  Y() { \
    std::cout << name << " ctor" << std::endl; \
  } \
  ~Y() { \
    std::cout << name << " dtor" …
Run Code Online (Sandbox Code Playgroud)

c++ linker compiler-bug

4
推荐指数
1
解决办法
301
查看次数

删除constexpr会更改gcc上数组的值

constexpr std::array我写道,当试图回答问题并创建一个问题时

// Own implementation as std::array::operator[] is not constexpr :/
template <typename T, std::size_t N>
struct array
{
    constexpr T& operator[] (std::size_t index) { return data[index];}
    constexpr const T& operator[] (std::size_t index) const { return data[index];}

    constexpr std::size_t size() const { return N; }

    T data[N];
};

constexpr array<std::size_t, 1001u> make_bottle_count()
{
    array<std::size_t, 1001u> a = {{0, 1, 2, 3, 4, 1, 2, 1, 2, 3, 1}};
    for (int i = 11; i != a.size(); ++i) {
        a[i] …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-bug constexpr c++14

4
推荐指数
1
解决办法
170
查看次数

如何调整 std::vector&lt;std::queue&lt;std::unique_ptr&lt;int&gt;&gt;&gt; 的大小?

我正在尝试执行以下操作:

#include <memory>
#include <vector>
#include <queue>

int main() {
    std::vector<std::queue<std::unique_ptr<int>>> v;
    v.resize(10);
}

Run Code Online (Sandbox Code Playgroud)

但是我在 GCC 10.2 中得到了这个:

$ g++ test.cpp -o test
In file included from /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/g++-v10/memory:66,
                from test.cpp:1:
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/g++-v10/bits/stl_uninitialized.h: In instantiation of '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = std::_Deque_iterator<std::unique_ptr<int>, const std::unique_ptr<int>&, const std::unique_ptr<int>*>; _ForwardIterator = std::_Deque_iterator<std::unique_ptr<int>, std::unique_ptr<int>&, std::unique_ptr<int>*>]':
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/g++-v10/bits/stl_uninitialized.h:325:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = std::_Deque_iterator<std::unique_ptr<int>, const std::unique_ptr<int>&, const std::unique_ptr<int>*>; _ForwardIterator = std::_Deque_iterator<std::unique_ptr<int>, std::unique_ptr<int>&, std::unique_ptr<int>*>; _Tp = std::unique_ptr<int>]'
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include/g++-v10/bits/stl_deque.h:896:36:   required from 'std::deque<_Tp, _Alloc>::deque(const …
Run Code Online (Sandbox Code Playgroud)

c++ gcc libstdc++ compiler-bug

4
推荐指数
1
解决办法
155
查看次数

为什么 scanf 解析“2E”而不解析“.” (使用GCC)作为浮点数的“匹配输入序列的前缀”?

注意:我的问题的原始版本与编译器无关,并假设 GCC(我用来进行实验)的行为完全正确,并且匹配输入序列的非空前缀不会导致匹配失败输入失败。事实证明(参见:C17 草案,7.21.6.2 \xc2\xb610),答案更有可能在编译器/库错误中找到,而不是在匹配的前缀的定义和正确处理的复杂性中找到。然而,为了保留问题的原始精神,我只是保守地对其进行了编辑(因此,最初的假设仍然在这篇文章的后半部分中体现出来)。

\n

考虑到这一点,这篇文章所涉及的问题的一个方面仍然没有解决,即:%4c示例(底部)中是否适合CD写入q[].

\n
\n

根据标准(C17 草案,6.4.4.2 \xc2\xb61),2E0(2.0) 和.5(0.5) 是有效的浮动常量,而2E.不是。

\n

然而,对于 GCC,scanf解析2E为 2.0,但它不会解析.为任何内容

\n
#include <stdio.h>\n\nint main(void) {\n    float fl;\n    char c;\n\n    printf("Please enter a floating-point number: ");\n    if (scanf("%f", &fl) == 1)\n        printf("<%.2f>\\n", fl);\n    if (scanf("%c", &c) == 1)\n        printf("[%c]\\n", c);\n\n …
Run Code Online (Sandbox Code Playgroud)

c floating-point scanf language-lawyer compiler-bug

4
推荐指数
1
解决办法
133
查看次数

为什么使用 clang 15 在向量上调用 std::ranges::rotate 会导致“没有匹配的函数调用 '__begin'”错误?

我有以下函数可以旋转 char 向量:

void rotate()
{
    std::ranges::rotate(_right, _right.begin() + 1);
}
Run Code Online (Sandbox Code Playgroud)

_right 定义为:

 std::vector<char> _right;
Run Code Online (Sandbox Code Playgroud)

尝试用 clang 15 编译它,它抱怨(https://godbolt.org/z/7ovTfxe31):

 no matching function for call to '__begin'
Run Code Online (Sandbox Code Playgroud)

原因似乎如下:

in instantiation of template type alias 'iterator_t' requested here
  requires contiguous_iterator<iterator_t<_Derived>>
Run Code Online (Sandbox Code Playgroud)

但我假设向量是一个连续的容器。代码使用 GCC 编译并运行。

这是我的问题:

  1. 根据cppreference,上述调用应该是合法的调用,还是我错过了什么?
  2. 如果这是一个合法的调用,那么有什么方法可以让 clang 像 GCC 一样接受调用呢?

c++ compiler-bug clang++ c++20

4
推荐指数
1
解决办法
250
查看次数

C++ - "最重要的const"不适用于表达式?

根据Herb Sutter的文章http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the-most-important-const/,以下代码是正确的:

#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> f() { return {{1},{2},{3},{4},{5}}; }

int main()
{
    const auto& v = f();
    cout << v[3][0] << endl;
}
Run Code Online (Sandbox Code Playgroud)

即生命周期v延长到vconst参考的生命周期.事实上,根据valgrind的说法,这可以很好地用gcc和clang编译而且没有泄漏.

但是,当我main这样改变功能时:

int main()
{
    const auto& v = f()[3];
    cout << v[0] << endl;
}
Run Code Online (Sandbox Code Playgroud)

它仍然编译,但valgrind警告我函数的第二行中的无效读取,因为第一行中的内存是空闲的.

这是符合标准的行为还是g ++(4.7.2)和clang(3.5.0-1~exp1)中的错误?

如果它符合标准,对我来说似乎很奇怪......哦.

c++ reference compiler-bug

3
推荐指数
1
解决办法
310
查看次数

不可能隐式移动操作?

据我所知[class.copy.ctor][class.copy.assign],A以下代码中的struct 不应该是可移动构造的,也不应该是move-assignable:

#include <type_traits>


struct X {
  X() noexcept; // user-declared default constructor
  ~X() noexcept; // Force X not to be trivially copyable
  X(X &&) = delete; // Explicitly deleted move constructor
  X(X const &) = delete; // Explicitly deleted copy constructor
  X & operator=(X &&) = delete; // Explicitly deleted move assignment operator
  X & operator=(X const &) = delete; // Explicitly deleted copy assignment op.
};
static_assert(!std::is_copy_constructible<X>::value, "");
static_assert(!std::is_copy_assignable<X>::value, "");
static_assert(!std::is_move_assignable<X>::value, ""); …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer compiler-bug move-semantics c++11

3
推荐指数
1
解决办法
158
查看次数

遍历C中的列表时是否看到优化错误?

我一直在努力将同事的软件库集成到我们的大型应用程序中。他一直在写作和下测试他的图书馆-O0gcc 4.9.3。这是用于警报系统的嵌入式软件。该错误是在-Os优化条件下观察到的,我们也在上使用了直接C语言(没有C ++废话)gcc 4.9.3。该体系结构是ARM Cortex-M4F。

我在将该代码集成到较大的堆栈中时遇到问题。以下代码最多应进行迭代,GLOBAL_MAX_DEVICES直到在表中找到可用空间以插入其条目为止:

RET_VALUES DEVICE_Add_To_New_Table( uint8_t *new_id, uint16_t unique_id )
{
    RET_VALUES ret_value = RET_OK;
    uint8_t pos = 0;

[...]
    else
    {
        debug_print( "A,", true, DEBUG_DEVELOP_MODE_PLAIN, DEBUG_TRACE );

        // See if the unique_id is already in the table (relearn)
        while(( unique_id != DEVICE_new_table.unique_id[pos] ) && ( pos < GLOBAL_MAX_DEVICES ))
        {
            debug_print( "B,", false, DEBUG_DEVELOP_MODE_PLAIN, DEBUG_TRACE );
            pos++;
        }
[...]
Run Code Online (Sandbox Code Playgroud)

我们遇到的问题是,它不是循环迭代GLOBAL_MAX_DEVICES次数(当前为13),而是迭代200多次。序列“ B”在while(( unique_id != …

c gcc compiler-optimization compiler-bug

3
推荐指数
1
解决办法
104
查看次数

Msvc 编译 &amp;(Class::mem) 但 gcc 不编译

我最近学习了指向成员语法的指针,然后编写了如下所示的程序,msvc 可以编译,但 gcc 和 clang 拒绝。

#include <iostream>
#include <type_traits> 
#include <concepts>
struct C
{
    int foo()
    {
    return 5;
    }
};
int main()
{ 
      int (C::*ptr)() = &(C::foo);   //msvc compiles but not gcc and clang  
}  
Run Code Online (Sandbox Code Playgroud)

我想知道哪一个是正确的。现场演示

c++ visual-c++ language-lawyer compiler-bug

3
推荐指数
1
解决办法
244
查看次数