Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Ambiguous call to overloaded function
    primarykey
    data
    text
    <p>I have the following code for a "safe" strncpy() -- basically it's wrapper automatically takes fixed array sizes for string buffers so you don't have to do the extra work to pass them in (and this convenience is safer because you won't accidentally type the wrong size for fixed array buffers).</p> <pre><code>inline void MySafeStrncpy(char *strDest,size_t maxsize,const char *strSource) { if(maxsize) { maxsize--; strncpy(strDest,strSource,maxsize); strDest[maxsize]=0; } } inline void MySafeStrncpy(char *strDest,size_t maxDestSize, const char *strSource, size_t maxSourceSize) { size_t minSize=(maxDestSize&lt;maxSourceSize) ? maxDestSize:maxSourceSize; MySafeStrncpy(strDest,minSize,strSource); } template &lt;size_t size&gt; void MySafeStrncpy(char (&amp;strDest)[size],const char *strSource) { MySafeStrncpy(strDest,size,strSource); } template &lt;size_t sizeDest,size_t sizeSource&gt; void MySafeStrncpy(char (&amp;strDest)[sizeDest], const char (&amp;strSource)[sizeSource]) { MySafeStrncpy(strDest,sizeDest,strSource,sizeSource); } template &lt;size_t sizeSource&gt; void MySafeStrncpy(char *strDest,size_t maxDestSize, const char (&amp;strSource)[sizeSource]) { MySafeStrncpy(strDest,maxDestSize,strSource,sizeSource); } </code></pre> <p>Using the code results in an error in Visual C++ 2008 when compiling:</p> <pre><code>char threadname[16]; MySafeStrncpy(threadname,"MainThread"); error C2668: 'MySafeStrncpy' : ambiguous call to overloaded function &gt; could be 'void MySafeStrncpy&lt;16,11&gt;(char (&amp;)[16],const char (&amp;)[11])' &gt; or 'void MySafeStrncpy&lt;16&gt;(char (&amp;)[16],const char *)' &gt; while trying to match the argument list '(char [16], const char [11])' </code></pre> <p>What am I doing wrong here?</p> <p>It seems like the compiler is unable to determine if the character string literal <code>"MainThread"</code> should be treated as a <code>const char *</code> or a <code>const char[11]</code> when determining which template function to call.</p> <p>I would like it to treat the string literal as a <code>const char[11]</code> and select the <code>void MySafeStrncpy&lt;16,11&gt;(char (&amp;)[16],const char (&amp;)[11])</code> variant since that is the "safest".</p> <p>Also two more constraints on answers: 1) I am unable to switch compilers (the code compiles on other compilers) and 2) the company will not allow me to use external template libraries for a solution.</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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