Search code examples
c++templatesitoa

itoa in a template function


Code goes first:

template <typename T>
void do_sth(int count)
{
    char str_count[10];
    //...
    itoa(count, str_count, 10);
    //...
}

but I got some compile-error like this:

error: there are no arguments to ‘itoa’ that depend on a template parameter, so a declaration of ‘itoa’ must be available
error: ‘itoa’ was not declared in this scope

But I indeed included <cstdlib>. Who can tell me what's wrong?


Solution

  • It appears that itoa is a non-standard function and not available on all platforms. Use snprintf instead (or type-safe std::stringstream).