spdlog icon indicating copy to clipboard operation
spdlog copied to clipboard

cannot bind packed field/cannot bind bitfield

Open andreaswesterlund opened this issue 4 years ago • 6 comments

Hi,

Since https://github.com/gabime/spdlog/pull/1726 I cannot log variables that have attribute packed. Same goes for bitfields.

See https://stackoverflow.com/questions/27491432/why-cant-i-return-a-reference-to-a-packed-field. So with perfect forwarding a reference to the variable is needed. But this is not possible if the struct is packed or if it is a bitfield.

Thanks for a great logging library!

andreaswesterlund avatar May 05 '21 06:05 andreaswesterlund

According to the so answer it is a bug in gcc?

gabime avatar May 05 '21 07:05 gabime

Hi,

I have the same issue. Had to back down spdlog to 1.8.0

hakan-t avatar May 05 '21 07:05 hakan-t

It is not a bug, GCC plays it safe and disallows this operation.

See https://groups.google.com/g/comp.lang.c++/c/umZJNJiN7JU

andreaswesterlund avatar May 05 '21 07:05 andreaswesterlund

I just updated the version of spdlog I'm using and ran into this issue. This appears to be a result of the change to using perfect forwarding of args: https://github.com/gabime/spdlog/commit/23572369fced02f41cc1cfef061540192df56986.

This change makes it so args are no longer guaranteed passed in as const and any temporaries that are created in a function call must be const qualified. (this is what the compiler is complaining about- a temporary is trying to be passed as non-const.)

so this got broken:

struct Foo{
  int a:24;
  int b:8;
};

int main(){
// ... make logger, etc.
Foo foo;
foo.a = 16;
foo.b = 10;

logger->debug("[Foo] a: {} b:{}", foo.a, foo.b); // compiler error

const Foo& foo_cref = foo;
logger->debug("[Foo] a: {} b:{}", foo_cref.a, foo_cref.b); // ok
}

What was the motivation to move to use perfect forwarding?

jasonbeach avatar May 26 '21 16:05 jasonbeach

The primary motivation was to fix #1725

gabime avatar May 26 '21 19:05 gabime

Same here. Upgraded to 1.8.5 and could not compile. Would love to get back the old behavior.

zzhang97 avatar May 27 '21 00:05 zzhang97