以下代码
#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 编译后因分段错误而崩溃。代码不标准还是叮当错了?
这段代码用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)