Why isn't std::array::size static?

后端 未结 6 2041
执念已碎
执念已碎 2020-12-15 16:57

The size of std::array is known at compile time, but the size member function isn\'t static. Is there any reason for that? It\'s slightly inconvenient not to be

6条回答
  •  遥遥无期
    2020-12-15 17:42

    array::size is constexpr, so unless the stored type has a constructor or destructor, the operation array_t().size() is very unlikely to have any runtime effect. You can embed it in a template argument to ensure it doesn't. It does otherwise look like runtime code, though.

    I think that it's nonstatic simply for uniformity with other containers. For example, you can form a pointer-to-member-function to it. Discovering the true rationale of anything often takes tough research, though. It could be that the authors never thought of it.

    The other thing that comes to mind is that some special functions such as operator () () cannot be static, so any opportunistic application of static can only be piecemeal. Generic problems are better solved in uniform fashion, even if it means changing the core language.

提交回复
热议问题