gpt4 book ai didi

c++ - 如何修复 -Wsubobject-linkage 警告?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:45:09 29 4
gpt4 key购买 nike

我在 clang 和 VC++ 中收到编译良好且无警告的代码的 gcc 警告,因此我假设它是特定于 gcc 的。这是代码:

namespace myns {
using TokenList = std::vector<size_t>;
using RuleList = std::vector<size_t>;
using RulePathPair = std::pair<size_t, TokenList>;
using CandidatesCollection = struct { std::map<size_t, TokenList> tokens; std::set<RulePathPair> rules; };

class A {
private:
CandidatesCollection _candidates;
};
} // namespace myns

警告是:

warning: 'myns::A' has a field 'myns::A::_candidates' whose type has no linkage [-Wsubobject-linkage]

这是什么意思以及如何消除警告?

最佳答案

我相信编译器在这里可能是错误的:CandidatesCollection 引用的类型实际上应该具有外部链接。

[basic.link]/4 ...A name having namespace scope that has not been given internal linkage above has the same linkage as the enclosing namespace if it is the name of

...

(4.3) — a named class (Clause 9), or an unnamed class defined in a typedef declaration in which the class has the typedef name for linkage purposes (7.1.3); ...


[dcl.typedef]/9 If the typedef declaration defines an unnamed class (or enum), the first typedef-name declared by the declaration to be that class type (or enum type) is used to denote the class type (or enum type) for linkage purposes only (3.5). [ Example:

 typedef struct { } *ps, S; // S is the class name for linkage purposes

—end example ]

因此,如果 CandidatesCollection 定义为

typedef struct { ... } CandidatesCollection;

这两段话清楚地表明,CandidatesCollection 命名的类很乐意拥有外部链接。

然后是

[dcl.typedef]/2 A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name. It has the same semantics as if it were introduced by the typedef specifier.

强调我的。这表明 using 引入的名称应该为未命名的类提供“用于链接目的的名称”以及等效的 typedef 声明,从而确保该类具有外部链接。

关于c++ - 如何修复 -Wsubobject-linkage 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41167119/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com