【发布时间】:2017-11-07 12:33:08
【问题描述】:
我正在构建一个包含一堆 lambda 的共享库,其中一些 lambda 是在其他 lambda 中创建的。但是,当我使用 -fvisibility=hidden 和 -Wall 时,我会收到关于声明具有更高可见性的东西的警告,我真的不明白。我有一个最小的例子:
#include <memory>
template<class T>
class MyClass {
public:
MyClass() {
#if 0
auto fn = [this] { /*Do something useful here*/ };
auto outer = [this,fn]() { /*use fn for something here*/ };
#else
auto outer = [this]()
{
auto fn = [this] { /*Do something useful here */ };
//use fn for something here
};
#endif
/* use outer for something */
}
};
int main() { MyClass<int> r; }
如果我编译它,我会收到以下警告:
% g++ -Wall -fvisibility=hidden -Wno-unused-but-set-variable -o visibility_test.cpp.o -c visibility_test.cpp
visibility_test.cpp: In instantiation of ‘struct MyClass<T>::MyClass()::<lambda()> [with T = int]::<lambda()>’:
visibility_test.cpp:13:22: required from ‘MyClass<T>::MyClass()::<lambda()> [with T = int]’
visibility_test.cpp:11:23: required from ‘struct MyClass<T>::MyClass() [with T = int]::<lambda()>’
visibility_test.cpp:11:14: required from ‘MyClass<T>::MyClass() [with T = int]’
visibility_test.cpp:22:27: required from here
visibility_test.cpp:13:32: warning: ‘MyClass<T>::MyClass()::<lambda()> [with T = int]::<lambda()>’ declared with greater visibility than the type of its field ‘MyClass<T>::MyClass()::<lambda()> [with T = int]::<lambda()>::<this capture>’ [-Wattributes]
auto fn = [this] { /*Do something useful here */ };
如果我将 #if 0 更改为 #if 1,从而将 fn 的创建移到“外部”lambda 之外,则一切编译正常。
当我在 Arch 盒子上安装 GCC 6 时,此警告开始出现。使用 6.3.1 和 7.1.1 编译时得到它。
所以,我的问题是:
- 这个警告想告诉我什么?
- 如何在不必过多违反我的代码的情况下摆脱警告(在我的示例中移动 lambdas 并不是一个真正的选择。)
更新:所以,我已经接受这是 GCC 中的一个错误,我现在想以最小的副作用摆脱警告。所以我在 MyClass 的构造函数中添加了“__attribute__ ((visibility ("default")))”,看起来效果不错。
【问题讨论】:
-
您尝试过 clang++ 吗……它有时可以提示您。