Why is this undefined behaviour?

Posted by xryl669 on Stack Overflow See other posts from Stack Overflow or by xryl669
Published on 2013-11-08T14:52:47Z Indexed on 2013/11/08 15:54 UTC
Read the original article Hit count: 175

Filed under:
|

Here's the sample code:

X * makeX(int index) { return new X(index); }
struct Tmp {
    mutable int count;
    Tmp() : count(0) {}
    const X ** getX() const { 
        static const X* x[] = { makeX(count++), makeX(count++) };
        return x; 
    }
};

This reports Undefined Behaviour on CLang version 500 in the static array construction. For sake of simplication for this post, the count is not static, but it does not change anything.

© Stack Overflow or respective owner

Related posts about c++

Related posts about clang