Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You got an error because the conversion is illegal. The reason it is illegal is simple: it breaks <code>const</code> without requiring a <code>const_cast</code>. </p> <p>As an example of why it is forbidden, imagine that <code>foo</code> was:</p> <pre><code>void foo( char const*&amp; str ) { str = "abc"; } int main() { char* h; foo( h ); *h = '1'; } </code></pre> <p>If you're not going to modify <code>str</code> in <code>foo</code>, pass by value. Pass by reference will works <em>if</em> you have <code>char const* const&amp;</code>, but there's no reason to use it here. It works because the additional const means that you can bind a temporary to it (as in the case of <code>foo( "hello" )</code>, where the argument is a temporary resulting from the conversion of <code>char const[6]</code> to <code>char const*</code>, and <code>foo( h )</code> works, because the implicit const conversions will work (in C++, but not in C!), as long as you add const everywhere (and not just at one level).</p> <p>Also, your code also uses a deprecated conversion to initialize <code>h</code>. You should get a warning here. And <code>void main</code> is an error, and shouldn't compile.</p> <h2>Edit:</h2> <p>Just to be clear, there's no problem with:</p> <pre><code>void f( char const*&amp; str ); </code></pre> <p>But you can only call it with an lvalue of type <code>char const*</code>; anything else will either result in an illegal implicit const conversion, or try to initialize a non-const reference with an rvalue, which is illegal.</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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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