Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>C++ does support it - well sort of:</p> <pre><code>vector&lt; string&gt; func() { vector&lt;string&gt; res; res.push_back( "hello" ); res.push_back( "world" ); return res; } </code></pre> <p>Even C sort-of supports it:</p> <pre><code>struct somearray { struct somestruct d[50]; }; struct somearray func() { struct somearray res; for( int i = 0; i &lt; 50; ++i ) { res.d[i] = whatever; } // fill them all in return res; } </code></pre> <p>A <code>std::string</code> is a class but when you say a string you probably mean a literal. You can return a literal safely from a function but actually you could statically create any array and return it from a function. This would be thread-safe if it was a const (read-only) array which is the case with string literals.</p> <p>The array you return would degrade to a pointer though, so you would not be able to work out its size just from its return. </p> <p>Returning an array, if it were possible, would have to be fixed length in the first place, given that the compiler needs to create the call stack, and then has the issue that arrays are not l-values so receiving it in the calling function would have to use a new variable with initialisation, which is impractical. Returning one may be impractical too for the same reason, atlhough they might have used a special notation for return values. </p> <p>Remember in the early days of C all the variables had to be declared at the top of the function and you couldn't just declare at first use. Thus it was infeasible at the time.</p> <p>They gave the workaround of putting the array into a struct and that is just how it now has to remain in C++ because it uses the same calling convention.</p> <p>Note: In languages like Java, an array is a class. You create one with new. You can reassign them (they are l-values). </p>
 

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