c++11 and packed structures, bitwise operation with -Wconversion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi all,

I have two problems (maybe I misunderstanding something, or maybe I don't know something).

1) Is it possible to pack structure in c++11 instead of using attribute packed (__attribute__((packed)))? There is alignas keyword for structures and classes but using alignas(1) doesn't change structure.
alignas with '1' value is accepted by compiler but default align is used.

2) What is going on third line exactly (for x86_64/i686/avr machine):

/*0:*/ typedef uint8_t u8;
/*1:*/ static u8 MAIN_DDR;
/*2:*/ constexpr u8 BV(u8 v) { return static_cast<u8>(1 << v); }
/*3:*/ inline void setPinAsOut(u8 PIN) { MAIN_DDR |= BV(PIN); }

I want use -Wconversion option, but this option raises warning
for third line:

warning: conversion to ‘u8 {aka unsigned char}’ from ‘int’ may alter its value [-Wconversion]
 inline void setPinAsOut(u8 PIN) { MAIN_DDR |= BV(PIN); }

But types operates on 8 bits, so why compiler uses here bigger type? Or maybe it's a bug?

This line can be resolved by:
MAIN_DDR = MAIN_DDR | BV(PIN);
or by adding static_cast, but IMO it's not good;-/
The same to & operator, but changing line to:
MAIN_DDR = MAIN_DDR & BV(PIN);
doesn't resolve problem.

I'm using compilers in versions: 4.7.2, 4.8.3, 4.9.

Regards
Zygmunt




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux