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

后端 未结 6 2024
执念已碎
执念已碎 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:33

    Note that the Microsoft Visual C++ doesn't currently support constexpr (http://msdn.microsoft.com/en-us/library/hh567368.aspx), so the following valid code won't work:

        array dog;
        array cat;
    

    The following class provides a compile time static variable:

    /**
    * hack around MSVC's 2012 lack of size for const expr
    */
    template 
    struct vcarray : public std::array {
        static const size_t ArraySize= N;
    };
    

    which can be used as:

    vcarray cat;
    vcarray dog;
    

提交回复
热议问题