Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I just tried the equivalent in g++ 4.5 C++0x mode for char arrays and it won't let me both define</p> <pre><code>template &lt;typename T&gt;void moresilly(const T v[],const char *description) </code></pre> <p>AND </p> <pre><code>template &lt;typename T&gt;void moresilly(const T *v,const char *description) </code></pre> <p>it claims both are the same type.</p> <p>I had a function:</p> <pre><code>template &lt;typename T&gt;void silly(const T &amp; v,const char *description) { cout&lt;&lt;"size of "&lt;&lt;description&lt;&lt;" is "&lt;&lt; sizeof(T)&lt;&lt;endl; moresilly(v,description); } </code></pre> <p>It correctly gets the size of an array if passed and of a pointer if passed, but I can't use moresilly to distinguish between pointer and array, so I can't tell a 4 character array from pointer to n characters.</p> <p>It might work, sort of, to have templates on T[1],T[2], T[3] etc. but there's already a post saying that different compilers handle that (or some similar case) differently and that gnu prefers a pointer match in C++11.</p> <p>... added later: After some experiment I found something that works in g++ 4.5</p> <pre><code>template &lt;typename T,size_t L&gt;void moresilly(const T (&amp;v)[L],const char *description) { cout&lt;&lt;description&lt;&lt;" is an array"&lt;&lt;endl; } template &lt;typename T&gt;void moresilly(const T *v,const char *description) { cout&lt;&lt;description&lt;&lt;" is a pointer"&lt;&lt;endl; } template &lt;typename T&gt;void moresilly(const T v,const char *description) { cout&lt;&lt;description&lt;&lt;" is a raw value"&lt;&lt;endl; } template &lt;typename T&gt;void silly(const T &amp; v,const char *description) { cout&lt;&lt;"size of "&lt;&lt;description&lt;&lt;" is "&lt;&lt; sizeof(T)&lt;&lt;endl; moresilly(v,description); } </code></pre> <p>with the following works properly</p> <pre><code> silly("12345","immediate string of 5 characters plus zero"); silly((const char *)"12345","immediate constant char pointer of 5 characters plus zero"); char testarray[]="abcdef"; silly(testarray,"char array of 6 characters plus zero"); const char testarray2[]="abcdefg"; silly(testarray2,"const char array of 7 characters plus zero"); </code></pre> <p>Note that if the first function is defined with "const T v[L]" instead of "const T (&amp;v)[L]" it doesn't work, never matching anything.</p> <p>So I solved your problem but don't expect this to work in other versions of the compiler including future ones. This is why I hate c++. Somehow the definition of the language is so unclear that compilers are full of unstable edge cases.</p> <p>This is a useful trick though, I may use it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload