digitalmars.D.learn - C Macro deeper meaning?
- Andrew Edwards (9/9) Jan 30 2016 If I understand correctly, this piece of code:
- Adam D. Ruppe (9/10) Jan 30 2016 I'm guessing the macro was there in C to silence compiler
- Andrew Edwards (2/13) Jan 30 2016 Okay, got it. Much appreciated.
- Alex Parrill (4/13) Jan 31 2016 Might want to change the function argument to `ref T v`, as
- Kagamin (2/5) Feb 01 2016 Now notUsed has an unused parameter v.
If I understand correctly, this piece of code: enum NOTUSED(v) do { (void)(1 ? (void)0 : ( (void)(v) ) ); } while(0) can be converted to the following in D: void notUsed(T)(T v) { return cast(void)0; }; since it always returns cast(void)0 regardless of the input. But it cannot be that simple, so what am I missing? Thanks, Andrew Edwards
Jan 30 2016
On Sunday, 31 January 2016 at 02:58:28 UTC, Andrew Edwards wrote:But it cannot be that simple, so what am I missing?I'm guessing the macro was there in C to silence compiler warnings about not using a return value. So I think your translation is ok: NOTUSED(somefunction(....)); still calls the function, so the behavior is the same. If I'm right, the macro was just about discarding the return value in such a way as to tell the compiler warning / lint program that you intentionally wanted to ignore it.
Jan 30 2016
On Sunday, 31 January 2016 at 03:13:46 UTC, Adam D. Ruppe wrote:On Sunday, 31 January 2016 at 02:58:28 UTC, Andrew Edwards wrote:Okay, got it. Much appreciated.But it cannot be that simple, so what am I missing?I'm guessing the macro was there in C to silence compiler warnings about not using a return value. So I think your translation is ok: NOTUSED(somefunction(....)); still calls the function, so the behavior is the same. If I'm right, the macro was just about discarding the return value in such a way as to tell the compiler warning / lint program that you intentionally wanted to ignore it.
Jan 30 2016
On Sunday, 31 January 2016 at 02:58:28 UTC, Andrew Edwards wrote:If I understand correctly, this piece of code: enum NOTUSED(v) do { (void)(1 ? (void)0 : ( (void)(v) ) ); } while(0) can be converted to the following in D: void notUsed(T)(T v) { return cast(void)0; }; since it always returns cast(void)0 regardless of the input. But it cannot be that simple, so what am I missing? Thanks, Andrew EdwardsMight want to change the function argument to `ref T v`, as struct postblits might run otherwise. Probably no reason to `return` either.
Jan 31 2016
On Sunday, 31 January 2016 at 02:58:28 UTC, Andrew Edwards wrote:void notUsed(T)(T v) { return cast(void)0; }; since it always returns cast(void)0 regardless of the input. But it cannot be that simple, so what am I missing?Now notUsed has an unused parameter v.
Feb 01 2016