Note that there are some explanatory texts on larger screens.

plurals
  1. POTemplate class constructor overload resolution
    primarykey
    data
    text
    <p>I have a simple issue with ctor overload resolution for a class template:</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; using namespace std; enum EnumTypeVal { READ, WRITE }; template &lt;class T&gt; class TemplateClassTest { public: TemplateClassTest(const string &amp; stringVal, T typeVal, EnumTypeVal e = READ, const string &amp; defaultStringVal = "default"); TemplateClassTest(const string &amp; stringVal, const char * charVal); TemplateClassTest(const string &amp; stringVal, EnumTypeVal e = READ, const string &amp; defaultStringVal = "default"); private: T type; }; template &lt;class T&gt; TemplateClassTest&lt;T&gt;::TemplateClassTest(const string &amp; stringVal, T typeVal, EnumTypeVal e, const string &amp; defaultStringVal) { type = typeVal; cout &lt;&lt; "In TemplateClassTest(const string &amp;, T, EnumTypeVal, " "const string &amp;)" &lt;&lt; endl; } template &lt;class T&gt; TemplateClassTest&lt;T&gt;::TemplateClassTest(const string &amp; stringVal, const char * charVal) { cout &lt;&lt; "In TemplateClassTest(const string &amp;, const char *)" &lt;&lt; endl; } template &lt;class T&gt; TemplateClassTest&lt;T&gt;::TemplateClassTest(const string &amp; stringVal, EnumTypeVal e, const string &amp; defaultStringVal) { cout &lt;&lt; "In TemplateClassTest(const string &amp;, EnumTypeVal, const string &amp;)" &lt;&lt; endl; } typedef TemplateClassTest&lt;long long unsigned int&gt; u32Type; typedef TemplateClassTest&lt;bool&gt; boolType; int main() { u32Type l("test", "0"); //matches ctor 2 u32Type v("test", 0); // ambiguity between ctor 1 and 2 boolType b("test", "true"); //matches ctor 2 return 0; } </code></pre> <p>The second call fails to compile by throwing error:</p> <blockquote> <p>Call of overloaded 'TemplateClassTest(const char [5], int) is ambiguous.</p> </blockquote> <p>Why does <code>int</code> matches to <code>const char *</code>? This situation can be solved by changing the <code>const char *</code> to <code>const string &amp;</code> in ctor 2. But doing so, <code>boolType b("test", "true")</code> now gets matched to ctor 1 instead of ctor 2.</p> <p>My requirements are:</p> <ul> <li><code>u32Type v("test", 0)</code> should match ctor 1</li> <li><code>boolType b("test", "true")</code> should match ctor 2.</li> </ul> <p>Limitations are:</p> <ul> <li>ctor 1 and 3 signatures can't be changed</li> <li>user code calls in main() can't be changed.</li> </ul> <p>Any help is highly appreciated..Thanks!</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