Adding const keyword to an array passed as a parameter to function

前端 未结 5 1031
死守一世寂寞
死守一世寂寞 2021-02-07 07:17

Is there any way that I can add const keyword to an array passed as a parameter to function:

void foo(char arr_arg[])

If I place <

5条回答
  •  时光说笑
    2021-02-07 08:00

    For C++, Mooing Duck's answer using the template is the most straight forward.

    If you have C code that is calling a C interface implemented in C++, you are stuck with converting the argument into a pointer argument instead, and making that const

    If you were to use Boost's array instead of using C arrays directly, then you would be able to make that const, although it would also be a template function:

    template 
    void foo (Boost::array const & arg) {}
    

    The advantage of Boost::array is that it gives you the ability to do a light weight allocation of an array off the stack, but be able to fully use STL algorithms that depend upon traits in the container.

提交回复
热议问题