Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC 6.x warning about lambda visibility

I am building a shared library that contains a bunch of lambdas, and some of those lambdas are created inside other lambdas. But, when I use -fvisibility=hidden and -Wall I get a warning about declarations of something with greater visibility, that I honestly do not 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 the #if 0 to #if 1, thereby moving the creation of fn to outside the "outer" lambda it all compiles fine.

This warning started appearing when I installed GCC 6 on my Arch box. I get it when compiling with 6.3.1 and 7.1.1.

So, my questions are:

  1. What is this warning trying to tell me?
  2. How do I get rid of the warning without having to violate my code too much (moving the lambdas like in my example is not really an option.)

Update: So, I have accepted that this is a bug in GCC, and I now wanted to get rid of the warning with minimal side effects. So I added "__attribute__ ((visibility ("default")))" to the constructor of MyClass, which appears to work nicely.

like image 344
DonOregano Avatar asked Jun 06 '17 13:06

DonOregano


1 Answers

Looks like it's a bug in gcc.

There is bug report and there were same warnings earlier without lambdas. You can handle this with using -fvisibility default, or manually setupping visibility to hidden/default by attribute.

like image 94
ForEveR Avatar answered Sep 22 '22 21:09

ForEveR