Nested struct breaks constexpr despite being identical to global ones

后端 未结 1 426
暗喜
暗喜 2020-12-30 02:21

I\'m having trouble with the following code:

template
constexpr int get(T vec) {
  return vec.get();
}

struct coord {
  constexpr int get(         


        
相关标签:
1条回答
  • 2020-12-30 03:26

    It is a constant expression.... eventually, as this shows you can see by moving i into main():

    • http://ideone.com/lucfUi

    The error messages are pretty clear what's going on, which is that foo::coord2::get() isn't defined yet, because member function definitions are delayed until the end of the enclosing class so that they can use members declared later.

    It's a little surprising that the definition is delayed until the end of the outermost enclosing class, but you'd be even more surprised if foo::coord2::get() couldn't access foo::g.

    The Standard agrees with the compiler, btw. Part of section 9.2p2 says

    Within the class member-specification, the class is regarded as complete within function bodies, default arguments, exception-specifications, and brace-or-equal-initializers for non-static data members (including such things in nested classes).

    Unfortunately, it's only inferred that the closing brace of the class declaration becomes the point-of-definition for these deferred regions. I believe it's a defect in the Standard that it doesn't say this explicitly.

    See also:

    • https://stackoverflow.com/a/11523155/103167
    0 讨论(0)
提交回复
热议问题