File: bitfield_packed_struct.cpp

package info (click to toggle)
doctest 2.4.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,436 kB
  • sloc: cpp: 13,546; python: 261; makefile: 30; sh: 1
file content (33 lines) | stat: -rw-r--r-- 801 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <doctest/doctest.h>

DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
#include <cstdint>
#include <sstream>
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END

DOCTEST_GCC_SUPPRESS_WARNING("-Wmissing-declarations")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-prototypes")

#ifndef WIN32
struct __attribute__((packed)) P {
    int i;
};
TEST_CASE("packed_struct") {
    P p;
    p.i = 0;
    CHECK(p.i == 0); // error: cannot bind packed field ‘s.S::i’ to ‘int&’
    //CHECK(int(s.i) == 0); // 'useless cast'
}
#else
#pragma pack(push, 1)
struct P {
    int i;
};
TEST_CASE("packed_struct") {
    P p;
    p.i = 0;
    CHECK(p.i == 0); // error: cannot bind packed field ‘s.S::i’ to ‘int&’
    //CHECK(int(s.i) == 0); // 'useless cast'
}
#pragma pack(pop)
#endif