Note that there are some explanatory texts on larger screens.

plurals
  1. POIs type checking for references in a template class less strict?
    primarykey
    data
    text
    <p>The following is allowd in C++ through promotion:</p> <pre><code>int ivalue = true; bool bvalue = 1; </code></pre> <p>Thats okay. And this is not allowed, through type-checking:</p> <pre><code>int&amp; ivalue = false; bool&amp; bvalue = 0; </code></pre> <p>Thats okay.</p> <p>Look on this, from Wikipedia. <a href="http://en.wikipedia.org/wiki/Property_(programming)#C.2B.2B" rel="nofollow">http://en.wikipedia.org/wiki/Property_(programming)#C.2B.2B</a></p> <pre><code>#include &lt;iostream&gt; template &lt;typename T&gt; class property { T value; public: T &amp; operator = (const T &amp;i) { ::std::cout &lt;&lt; "T1: " &lt;&lt; i &lt;&lt; ::std::endl; return value = i; } // This template class member function template serves the purpose to make // typing more strict. Assignment to this is only possible with exact identical // types. template &lt;typename T2&gt; T2 operator = (const T2 &amp;i) { ::std::cout &lt;&lt; "T2: " &lt;&lt; i &lt;&lt; ::std::endl; T2 &amp;guard = value; return value = i; throw guard; // Never reached. }/**/ operator T const &amp; () const { return value; } }; struct Bar { // Using the property&lt;&gt;-template. property &lt;bool&gt; alpha; property &lt;unsigned int&gt; bravo; }; int main () { Bar bar; bar.alpha = true; bar.bravo = true; // This line will yield a compile time error // due to the guard template member function. ::std::cout &lt;&lt; foo.alpha &lt;&lt; ", " &lt;&lt; foo.bravo &lt;&lt; ", " &lt;&lt; bar.alpha &lt;&lt; ", " &lt;&lt; bar.bravo &lt;&lt; ::std::endl; bool bvar = 22; int ivar = true; //int &amp;newvar = bvar; print(bvar); ::std::cout &lt;&lt; bvar &lt;&lt; " and " &lt;&lt; ivar &lt;&lt; "\n"; return 0; } </code></pre> <p>I think with using of templates the type-checking for references get lost? Am I right?</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.
 

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