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 <
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.