clang의 -Wweak-vtables의 의미는 무엇입니까?
나는 기본적으로 clang의 -Wweak-vtables
. 지금까지 관찰 한 내용은 다음과 같습니다.
사례 1 : (경고를 유발 함)
class A {
public:
virtual ~A(){}
};
class B : public A {
public:
virtual ~B(){}
};
int main(){}
사례 2 : (경고를 트리거하지 않음)
class A {
public:
virtual ~A(){}
};
int main(){}
사례 3 : (경고를 트리거하지 않음)
class A {
public:
virtual ~A();
};
A::~A(){}
class B : public A {
public:
virtual ~B(){}
};
int main(){}
사례 4 : (트리거 경고)
class A {
public:
virtual ~A(){}
virtual void fun(){}
};
class B : public A {
public:
virtual ~B(){}
};
int main(){}
사례 5 : (경고를 트리거하지 않음)
class A {
public:
virtual ~A(){}
virtual void fun();
};
class B : public A {
public:
virtual ~B(){}
};
int main(){}
사례 6 : (경고를 트리거하지 않음)
class A {
public:
virtual ~A(){}
virtual void fun(){}
};
class B : public A {};
int main(){}
사례 7 : (경고를 트리거하지 않음)
class A {
public:
virtual ~A(){}
virtual void fun(){}
};
class B : public A {
public:
virtual void fun(){}
};
int main(){}
정확한 경고는
warning: 'A' has no out-of-line virtual method definitions; its vtable
will be emitted in every translation unit [-Wweak-vtables]
따라서 클래스에서 인라인이 아닌 가상 함수를 선언하지 않으면 파생 클래스에 가상 소멸자가있는 경우에만 일종의 문제가 발생합니다.
질문 :
- 이것이 왜 문제입니까?
- Why does this get fixed by declaring a virtual function? (Warning speaks of definitions)
- Why does the warning not occur when I do not derive from the class?
- Why does the warning not occur when the derived class does not have a virtual destructor?
If all of a class's virtual
methods are inline, the compiler has no way to select a translation unit in which to place a single shared copy of the vtable — instead, a copy of the vtable has to be placed in each object file that needs it. On many platforms the linker is able to unify these multiple copies, either by discarding duplicate definitions or by mapping all references to one copy, so this is only a warning.
Implementing a virtual
function out-of-line enables the compiler to select the translation unit that implements that out-of-line method as a "home" for the class's implementation details, and places the single shared copy of the vtable in the same translation unit. If multiple methods are out-of-line, an arbitrary choice of method may be made by the compiler so long as that choice is determined only by the class's declaration; for example, GCC chooses the first non-inline method in declaration order.
If you don't override any method of a class, the virtual
keyword has no observable effect, so there's no need for the compiler to emit a vtable for the class. If you do not derive from A
, or if you fail to declare a derived class's destructor virtual
, there are no overridden methods in A
and thus A
's vtable is omitted. If you declare an additional out-of-line virtual
method to suppress the warning and also do something that overrides a method in A
, the implementation of the non-inline virtual
(and its accompanying copy of the vtable) needs to be supplied in a linked translation unit, otherwise linking will fail because the vtable is missing.
참고URL : https://stackoverflow.com/questions/23746941/what-is-the-meaning-of-clangs-wweak-vtables
'code' 카테고리의 다른 글
SQL Server Management Studio에서 SSIS 패키지를 보려면 어떻게합니까? (0) | 2020.10.30 |
---|---|
예외 발생 대 함수에서 None 반환? (0) | 2020.10.30 |
React Native에서 백그라운드 작업을 어떻게 실행할 수 있습니까? (0) | 2020.10.30 |
HTML5 Canvas 요소에서 사용자 정의 글꼴을 사용하려면 어떻게해야합니까? (0) | 2020.10.30 |
Java HTTPResponse에서 JSON을 어떻게 구문 분석합니까? (0) | 2020.10.30 |