type defined in free function, accessible through auto outside. Language Bug or Feature?

前端 未结 1 1396
灰色年华
灰色年华 2021-01-20 20:49

Let\'s define a class inside a free function, and access it outside:

#include 
auto myFunc(){
    class MyType{public: int i = 0; int j = 1;}         


        
相关标签:
1条回答
  • 2021-01-20 20:59

    The standard doesn't say anything about this specifically, except that — as you've already pointed out — it's the name that has a scope, not the type. Use of auto bypasses the type's name, giving you a way to get at the type regardless of the name's scope.

    It's kind of similar to how making a nested class private doesn't mean you can't use instances of it, only that you can't name it outside of the encapsulating class's scope.

    I don't see how you'd "prevent" it, nor why you'd want to.

    0 讨论(0)
提交回复
热议问题