VLAs and cast expressions

Quick question about VLAs: does the following evaluate the printf? Or
is the printf only evaluated if the result is needed? Or is this
illegal for some reason I'm forgetting?

void* a(void* x) {return (int (*)[10][printf("asdf")])x;}

Similar case which definitely needs to evaluate the printf:

void* a(void* x) {return (*(int (*)[10][printf("asdf")])x)+1;}

-Eli

I believe the code below is legal.

It seems like printf() should always be called, however I couldn't find a spec reference to validate this.

snaroff

Steve Naroff wrote:-

I believe the code below is legal.

It seems like printf() should always be called, however I couldn't
find a spec reference to validate this.

snaroff

I agree it's evaluated. I think 6.8.3p2 is all you need.

Neil.

Quick question about VLAs: does the following evaluate the printf?

gcc does not evaluate it. :frowning: After reading up on it, I can't find a good reason and plenty of reasons why it has to be evaluated.

I've filed 35198 – [4.2/4.3/4.4/4.5 Regression] missed evaluation of VM array type when used as a cast to get gcc fixed so that it agrees with clang. :slight_smile: If they disagree, they'll be able to provide a DR or pointers to the wording.

I think for all the hairy corners where we find differences between clang and gcc, we should file bug reports so that in the end, we can accept close to the same language, at least in pedantic mode.

Similar case which definitely needs to evaluate the printf:

Agreed.

Okay... I don't recall running into any other issues off the top of my
head, besides gcc's incorrect handling of integer constant expressions
and other craziness with constant expressions, which the gcc devs
already know about.

-Eli

FWIW, ICC prints "asdf" in both cases.

-bw