Yesterday I was surprised to find some code that seemed to consider char[] as a type:
typedef std::unique_ptr<char[]> CharPtr;
I would write something like:
typedef std::unique_ptr<char*, CharDeleter> CharPtr;
After some research, I found that the char[] syntax works, because std::unique_ptr provides a specialized specialization for processing arrays (for example, it will automatically call delete[] on the array without the need for user deletion)
But what does char[] really mean in C ++?
I saw syntax like:
const char a[] = "Constant string";
Here is how I interpret these examples:
Example 1 allocates a static array (on the stack), and empty indexes are valid, since the true size of the string is known at compile time (for example, the compiler basically handles the length for us behind the scenes)
Example 2 dynamically allocates 5 adjacent characters, with the first character stored at the address stored in p.
Example 3 defines a function that takes an array of size 10 as a parameter. (Behind the scenes, the compiler treats the array as a pointer) - for example. this is mistake:
void foo(char test[5]) {} void foo(char * test) {}
because function signatures are ambiguous for the compiler.
It seems to me that I understand the differences between arrays and pointers and their similarities. My confusion probably stems from my lack of experience creating / reading templates in C ++.
I know that specializing a template basically allows you to use a custom template (based on a specific template) depending on the parameters of the template type. Is char[] just a syntax available for template specialization (invoking a specific specialization)?
Also, what is the name for the types array, for example char[] ?