Note that there are some explanatory texts on larger screens.

plurals
  1. POexplicit copy constructor ignored even if exact argument was provided
    primarykey
    data
    text
    <p>The copy constructor has been provided. While using it exactly same type is passed to argument. Still the it seems the compiler( gcc/g++ 4.8.2) ignores existence of explicit copy constructor. The code generates compilation error. Why? The error is: </p> <pre><code>t.cpp: In function ‘A f(const A&amp;)’: t.cpp:19:12: error: no matching function for call to ‘A::A(const A&amp;)’ return a; //compilation error with gcc 4.8.2 ^ t.cpp:19:12: note: candidate is: t.cpp:14:5: note: A::A() A(){} ^ t.cpp:14:5: note: candidate expects 0 arguments, 1 provided t.cpp:21:1: warning: control reaches end of non-void function [-Wreturn-type] } ^ </code></pre> <p>I have already gone through<br> <a href="https://stackoverflow.com/questions/15049542/why-i-am-not-able-to-invoke-explicit-a-string-x">Why I am not able to invoke &#39;explicit a (string x)&#39;?</a> and <a href="https://stackoverflow.com/questions/11480545/explicit-copy-constructor">Explicit copy constructor</a>. Based on the those links and i have tried the forcing copy construction (to avoid optimization,Refer to my commented code).</p> <pre><code>#include &lt;iostream&gt; using namespace std; class A { public: explicit A(const A&amp; a) // an explicit copy constructor //A(const A&amp;) // an copy constructor, pretty much default one. //using it solves any compile problem. { cout &lt;&lt; "\nin the copy constructor" &lt;&lt; endl; } A(){} }; ////////////////////////////////// A f(const A &amp;a) { return a; //compilation error with gcc 4.8.2 //return A(a); // even tried this to avoid optimization. does not work } /////////////////////////////////// int main() { //A a; //f(a); return 0; } </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