7

The following code gives me this error when compiled with avr-g++ compiler ignoring packed attribute because of unpacked non-POD field 'float& foo::BAR'

what is the reason?

class foo {  
 public:       
     foo(float &bar);    
 private:  
     float &BAR;  
};

foo::foo(float &bar):BAR(bar)  
{

}

int main()  
{  
 float something;  
 foo fooobject(something);  
}
mic
  • 1,165
  • 1
  • 9
  • 8

2 Answers2

6

It appears to be a compiler bug : https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58798.

matovitch
  • 1,264
  • 11
  • 26
2

I don't know about your case in particular, but just to clarify for other Googlers, the warning makes sense when you actually __attribute__((packed)) as in:

#include <type_traits>

struct C {
    int i;
};

struct D : C {
    int j;
};

struct E {
    D d;
} __attribute__((packed));

static_assert(std::is_pod<C>());
static_assert(!std::is_pod<D>());
static_assert(!std::is_pod<E>());

then with GCC 8.1.0:

g++-8 -std=c++17 -c a.cpp

it warns:

a.cpp:12:7: warning: ignoring packed attribute because of unpacked non-POD field ā€˜D E::d’
     D d;
       ^

because as explained at: What are POD types in C++? the data contained inside PODs is not well defined by the standard, and therefore intuitively __attribute__((packed)) won't give you much in that case.

In particular, the reference member in your example makes foo non-POD as of C++17, although of course you are not using __attribute__((packed)) so the warning does not make sense.

I cannot reproduce the warning with your code on Ubuntu 16.04 AMD64.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • @harper no, I only checked x86 native, did not touch or know anything about AVR :-) – Ciro Santilli OurBigBook.com Jul 15 '20 at 12:59
  • @harper you can look a it like that. Or you can look at it like "I helped hundreds of Googlers, because most of them don't care about avr, only the error message. If you feel like downvoting, I don't mind. I'll eventuall help 1000 more googlers and gt that back and beat top answer. But please don't delete. If delete I'll have to waste 2 minutes creating a new Q/A pair, and half of Googlers will continue clicking on the one they don't care about for a while. – Ciro Santilli OurBigBook.com Jul 15 '20 at 13:23