Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to specify NULL as pointer-type argument to template function
    primarykey
    data
    text
    <p>This shows the gist of it:</p> <pre><code>#include &lt;utility&gt; class A { public: A() { } }; class B { public: B() { } }; typedef std::pair&lt;A*, B*&gt; ABPair; int main(int argc, char* argv[]) { B* b = 0; // no C2440 ABPair p2(new A(), b); ABPair p1(new A(), 0); // C2440 return 0; } </code></pre> <p>Is there a better way to make the p1 declaration work than just forcing a cast, e.g. <code>ABPair p1(new A(), (B*)NULL)</code>? This seems like it would be pretty common &amp; that there would be a "right" way to do it .. and that casting it is not the right way.</p> <p>On VS 2010, here's the full error:</p> <pre><code>1&gt;ClCompile: 1&gt; test.cpp 1&gt;c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(163): error C2440: 'initializing' : cannot convert from 'int' to 'B *' 1&gt; Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast 1&gt; c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(247) : see reference to function template instantiation 'std::_Pair_base&lt;_Ty1,_Ty2&gt;::_Pair_base&lt;_Ty,int&gt;(_Other1 &amp;&amp;,_Other2 &amp;&amp;)' being compiled 1&gt; with 1&gt; [ 1&gt; _Ty1=A *, 1&gt; _Ty2=B *, 1&gt; _Ty=A *, 1&gt; _Other1=A *, 1&gt; _Other2=int 1&gt; ] 1&gt; c:\users\charliearnold\documents\visual studio 2010\projects\test\test\test.cpp(20) : see reference to function template instantiation 'std::pair&lt;_Ty1,_Ty2&gt;::pair&lt;A*,int&gt;(_Other1 &amp;&amp;,_Other2 &amp;&amp;)' being compiled 1&gt; with 1&gt; [ 1&gt; _Ty1=A *, 1&gt; _Ty2=B *, 1&gt; _Other1=A *, 1&gt; _Other2=int 1&gt; ] 1&gt;c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(163): error C2439: 'std::_Pair_base&lt;_Ty1,_Ty2&gt;::second' : member could not be initialized 1&gt; with 1&gt; [ 1&gt; _Ty1=A *, 1&gt; _Ty2=B * 1&gt; ] 1&gt; c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(167) : see declaration of 'std::_Pair_base&lt;_Ty1,_Ty2&gt;::second' 1&gt; with 1&gt; [ 1&gt; _Ty1=A *, 1&gt; _Ty2=B * 1&gt; ] 1&gt; 1&gt;Build FAILED. </code></pre>
    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.
 

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