Is something wrong with wrapper classes for C string functions?
<p>I have some C++ code written in C-style.<br>
For some reasons I can't use C++ string and IO libraries, so for strings handling I should use only functions like <code>sprintf</code>, <code>itoa</code>, etc.</p>

<p>I want to replace C-style which requires temporary buffers
    char buf[12];
    itoa(x, buf, 16);
    set_some_text(buf);</p>

<p>by the following code</p>

<pre><code>class i2a
{
public:
    explicit i2a(int value) { ::sprintf(buf, "%d", value); }
    operator const char* () const { return buf; }
private:
    char buf[12];
};

// usage:
set_some_text(i2a(x));
</code></pre>

<p>(Such classes can be written for <code>char&lt;-&gt;wchar_t</code> convertions, etc.)</p>

<p>I see some cases when such classes will be dangerous: 
For example, one can write</p>

<pre><code>const char* someMeaningfulName = i2a(x);
// the right code should be i2a someMeaningfulName(x); or i2a someMeaningfulName = i2a(x);
set_some_text(someMeaningfulName);
</code></pre>

<p>In more complex case, a function which accepts text will not copy it, but will save pointer to it somewhere. For example it may be</p>

<pre><code>class Foo { .... const char* p; };
Foo f(const char* text) { ... foo.p = text; return foo; }
</code></pre>

<p>it can be really unobvious, unlike <code>const char*</code> variable.</p>

<p>Is there a way to make such classes more secure?</p>

<hr>

<p><strong>Upd</strong>: why not std::string, boost::lexical_cast, boost::format, etc :<br>
The code should work when compiled with -fno-except (C++ exceptions disabled - no <code>throw</code>, no stack unwinding). Also it should keep working on low memory conditions.<br>
<code>std::string</code>, streams uses heap-allocated memory and at least throws <code>bad_alloc</code>.<br>
When we have no free heap memory, usually we still have some kilobytes of stack (for example to write to user that we are out of memory and then make proper cleanup).</p>
