GCC 6.x Lambda Visibility Warning
I am creating a shared library that contains a bunch of lambdas and some of these lambdas are created inside other lambdas. But when I use -fvisibility = hidden and -Wall, I get a warning about ads for something with more visibility, which I honestly don't understand. I have a minimal example:
#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; }
If I compile this, I get the following warning:
% 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 I change #if 0 to #if 1, thereby moving the fn creation outside of the "outer" lambda, it all compiles fine.
This warning came up when I installed GCC 6 in the Arch box. I get it when compiling with 6.3.1 and 7.1.1.
So my questions are:
- What is this warning trying to tell me?
- How do I get rid of the warning without breaking too much code (moving the lambda as in my example is not really an option).
Update: So, I admitted that this is a bug in GCC and now I want to get rid of the warning with minimal side effects. So I added "__attribute__ ((visibility (" default ")))" to the MyClass constructor, which looks good.
source to share
Looks like a bug in gcc.
There is a bug report and it had the same warnings without lambdas before. You can handle this with the -fvisibility
default or manually adjust the visibility for the hidden / default attribute.
source to share