Is there a way to enforce full initialization of std::array

后端 未结 3 1617
耶瑟儿~
耶瑟儿~ 2021-01-02 05:18

I am using std::array (N is a fixed template-variable).

#include
template
struct A{
   size_t fun         


        
3条回答
  •  遥遥无期
    2021-01-02 05:48

    You can create a wrapper for your objects

    template 
    struct WrapT
    {
        WrapT() = delete;
    
        WrapT(T e)   : value(e){}
    
       public: T value; 
       // or add some operator()
    };
    

    and

    size_t function(std::array, N> arr){ return arr[N-1].value;}
    

    so a function call like (with full brace-init)

    function({ {{1}, {2}, {3}, {4}} }); 
    

    will not compile because of use of deleted function. Live example. The syntax is a little clumsy, and even then I'm not sure all the possible value-initialization cases are covered.

    @dpy points out you may omit the innermost ones, so you get to your original code: function( {{ 1, 2, 3, 4, 5 }} ).

提交回复
热议问题