Can somebody tell me the difference between #pragma pack(push,1)
and __attribute__((packed))
? I am getting a compilation error if I use second type of struct packing which says
cannot bind packed field ‘ABC.abc::a’ to ‘unsigned int&’
But if I use first type of struct packing then there is no compilation error.
This is my sample code:
//DataStructure.h
#ifndef DATASTRUCTURE_H_
#define DATASTRUCTURE_H_
struct abc
{
unsigned int a;
float b;
}__attribute__((packed));
#endif /* DATASTRUCTURE_H_ */
//Main.cpp
#include<iostream>
#include<map>
#include "DataStructure.h"
int main()
{
struct abc ABC;
ABC.a=3;
ABC.b=4.6f;
std::map<unsigned int,struct abc> Mapp;
Mapp.insert(std::make_pair(ABC.a,ABC));
}
The error comes from:
std::make_pair(ABC.a,ABC);
Since C++11, make_pair
is defined as:
template< class T1, class T2 >
std::pair<V1,V2> make_pair( T1&& t, T2&& u );
so giving it ABC.a
as first argument is trying to bind a "non-const" reference to a bitfield (what a packed struct is basically), which is illegal.
To solve that, you must create a fresh unsigned int and call make_pair
with it:
unsigned int a = ABC.a;
Mapp.insert(std::make_pair(a,ABC));