代码之家  ›  专栏  ›  技术社区  ›  Vuwox

避免缩小c样式数组创建时的警告范围

c++
  •  0
  • Vuwox  · 技术社区  · 5 年前

    int arr[3] = { 0.0f, 1.0f, 2.0f};
    

    post ,我能够创造

    template <class T, class ... Ts>
    std::array<T, sizeof...( Ts )> make_non_narrowed_arr(Ts&& ... ts)
    {
        return{ { static_cast<T>( ts )... } };
    }
    
    auto arr = make_not_narrowed_arr<int>(0.0f, 1.0f, 2.0f );
    

    template<class T, class ... Ts>
    T[sizeof...( Ts )] make_non_narrowed_arr(Ts&& ... ts) {...};
    
    template<class T, std::size_t N, class ... Ts>
    T[N] make_non_narrowed_arr(Ts&& ... ts) {...};
    
    template<class T, class ... Ts>
    T* make_non_narrowed_arr(Ts&& ... ts) {...};
    

    我想执行以下操作以获得一个无警告程序,而不在编译器中抑制它们。那么这个 post

    我不能换成 std::array T*

    这里是一个示例(不是确切的代码100%),我有什么样的代码,我想改变什么。

    void threshold(Image img, Image dst, float* th)
    {
        // Sanitty ckeck on image, and bla bla...
    
        // Perform the call to ipp
        IppStatus sts = (IppStatus)0;
        switch(img.nbChan)
        {
            case 1:
            {
                switch(img.type)
                {
                    case BYTE:
                    {
                        Ipp8u arr[1] = {Th[0] };
                        sts = ippiThreshold_GTVal_8u_C1R(img.data, img.pitch, dst.data, dst.pitch, arr);
                        break;
                    }
                    case FLOAT:
                    {
                        Ipp32f arr[1] = {Th[0] };
                        sts = ippiThreshold_GTVal_32f_C1R(img.data, img.pitch, dst.data, dst.pitch, arr);
                        break;
                    }
                }        
                break;
            }
            case 3:
            {
                switch(img.type)
                {
                    case BYTE:
                    {
                        Ipp8u arr[3] = {Th[0], Th[1], Th[2]};
                        sts = ippiThreshold_GTVal_8u_C3R(img.data, img.pitch, dst.data, dst.pitch, arr);
                        break;
                    }
                    case FLOAT:
                    {
                        Ipp32f arr[3] = {Th[0], Th[1], Th[2]};
                        sts = ippiThreshold_GTVal_32f_C3R(img.data, img.pitch, dst.data, dst.pitch, arr);
                        break;
                    }
                } 
                break;
            }
        }
    }
    
    1 回复  |  直到 5 年前
        1
  •  3
  •   Xirema    5 年前

    只用 std::array . 您断言由于需要与C风格的API进行接口而无法这样做,但这并不意味着您需要在代码中使用C风格的数组。

    //C Header
    void do_something(void * arr, size_t size);
    
    //C++ Code
    auto arr = make_non_narrowed_array<int>(0.f, 1.f, 2.f);
    //If size is meant to be the memory footprint
    do_something(arr.data(), sizeof(arr));
    //If size is meant to be the number of elements
    do_something(arr.data(), arr.size());
    

    template<typename T, size_t N> 
    void cpp::do_something(std::array<T, N> const& arr) {
        do_something(arr.data(), arr.size() * sizeof(T));
    }
    
        2
  •  0
  •   Vuwox    5 年前

    最后,在所有这些讨论之后,我重新审视了我的问题,并尝试做一些简单的事情来满足我的需要,尽管这不是一个超一般的函数。

    template<typename T, std::size_t N>
    void no_narrow(float * const in, T(&out)[N])
    {
        for ( int i = 0; i < N; i++ )
            out[i] = static_cast<T>( in[i] );
    }
    

    因此我称之为:

    Ipp8u arr[3];
    no_narrow(th, arr);