小编til*_*lin的帖子

在作为静态成员包含在另一个类中的类的构造函数中使用 cout

以下代码

#include <iostream>

struct A {
    A() {
        std::cout << std::endl;
    }
};

struct B {
    static inline A a;
};

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

用gcc 编译后成功,但用clang 编译后因分段错误而崩溃。代码不标准还是叮当错了?

https://godbolt.org/z/tEvfrW

c++ clang initialization-order language-lawyer

8
推荐指数
1
解决办法
127
查看次数

继承构造函数时GCC编译错误

这段代码用gcc编译有错误

template<typename>
struct B {
};

template<typename... Types>
struct A : public B<Types>... {
    using B<Types>::B...;
    using B<Types>::operator=...;
}
Run Code Online (Sandbox Code Playgroud)

编译器输出

<source>:8:8: error: expected nested-name-specifier before 'B'

    8 |  using B<Types>::operator=...;

      |   

 ^
Run Code Online (Sandbox Code Playgroud)

但是这段代码编译没有错误

template<typename>
struct B {
};

template<typename... Types>
struct A : public B<Types>... {
    using B<Types>::operator=...;
    using B<Types>::B...;
};
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会发生这种情况。


更新

对于 gcc,构造函数继承也会破坏代码

template<typename T>
struct B {
    void foo() {}
};

template<typename... Types>
struct A : public B<Types>... {
    using B<Types>::B...;

    void bar() {
        (B<Types>::foo() , ...); …
Run Code Online (Sandbox Code Playgroud)

c++ gcc

6
推荐指数
1
解决办法
79
查看次数

标签 统计

c++ ×2

clang ×1

gcc ×1

initialization-order ×1

language-lawyer ×1