5

I have the following code:

struct Literal
{
    int val;
    constexpr Literal(int const& val) : val(val) {}
    constexpr Literal(Literal const& rhs) : val(rhs.val) {}
};


struct Parent
{
    struct StaticObject
    {
        Literal const zero;

        constexpr StaticObject() :zero(0) {}
    };
    static constexpr StaticObject outer{};
};

The line 'static constexpr StaticObject outer{};' gives me the error:

'expression did not evaluate to a constant'

This is followed by

note: failure was caused by call of undefined function or one not declared 'constexpr'

note: see usage of 'Parent::StaticObject::StaticObject'

As far as I can tell, all the functions being used here are both defined and declared constexpr. Am I missing something, or is this a compiler bug?

Community
  • 1
  • 1
user1646801
  • 259
  • 2
  • 7
  • 3
    interesting, if you move `StaticObject` to global namespace, it compiles. and [this is a further reduced case](http://coliru.stacked-crooked.com/a/dde91c6c1ae44dfc) – Bryan Chen Jul 18 '16 at 23:37
  • Sounds like a bug, but whether in compiler or in current standard... Hm. – Cheers and hth. - Alf Jul 18 '16 at 23:48
  • 1
    @max66 Indeed, though note that when I looked at the question, both answers had the same score, so I'm not sure which of the two you meant by "same answer". I upvoted ecatmur's answer because I think it's more helpful. – Kyle Strand Jul 18 '16 at 23:53
  • a better answer as to why this is disallowed is answered [here](http://stackoverflow.com/questions/11522399/constexpr-initializing-static-member-using-static-function/11523155#11523155) – GreatAndPowerfulOz Jul 19 '16 at 15:42

0 Answers0