template < int _OutSize, typename _StringStream >
typename _StringStream::_Mystr itoa_ex( int _Value )
{
_StringStream Stream;
Stream << _Value << std::endl;
typename _StringStream::_Mystr Str;
Str = Stream.str();
typename _StringStream::_Mystr::size_type Size;
Size = Str.size() - 1;
if( Size < _OutSize )
{
Str.insert(0, "00000000000000000000000000000", _OutSize - Size );
}
return Str;
}
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::cout << itoa_ex<12, std::stringstream>( 10 ).c_str() << std::endl;
std::wcout << itoa_ex<12, std::wstringstream>( 10 ).c_str() << std::endl;
}
templates freak out on gcc...
This code freaks out on gcc, and I don't think I'm able to see why...
anyone?
That's not much information. Is it a runtime error? A compile-time error? If so, what error(s) does it give?
Yes, I expect it would...
First of all, don't prefix your template parameters with underscores. Identifiers starting with an _ and a capitol letter are reserved for standard library implementors.
Secondly, you are using an implementation specific typedef, _Mystr.
First of all, don't prefix your template parameters with underscores. Identifiers starting with an _ and a capitol letter are reserved for standard library implementors.
Secondly, you are using an implementation specific typedef, _Mystr.
Try:
Also, yours doesn't deal with negative correctly. If you don't want to handle negatives, use an unsigned type.
#include <iostream>#include <string>#include <sstream>#include <cmath>template<std::size_t OutSize, typename CharType>std::basic_string<CharType, std::char_traits<CharType> > itoa_ex(int value) { std::basic_stringstream<CharType, std::char_traits<CharType> > ss; ss<<std::abs(value); std::basic_string<CharType, std::char_traits<CharType> > str = ss.str(); std::size_t size = str.size(); if(value >= 0) { if(size - 1 < OutSize) { str.insert(0, OutSize - size, '0'); } } else { if(size - 2 < OutSize) { str.insert(0, OutSize - size - 1, '0'); str.insert(0, 1, '-'); } } return str;}int main(){ std::wcout << itoa_ex<5, wchar_t>(-10) << std::endl;}
Also, yours doesn't deal with negative correctly. If you don't want to handle negatives, use an unsigned type.
Quote:Original post by Washu
Yes, I expect it would...
First of all, don't prefix your template parameters with underscores. Identifiers starting with an _ and a capitol letter are reserved for standard library implementors.
Secondly, you are using an implementation specific typedef, _Mystr.
oh thanks, I didn't see it, my bad, but gcc gave me the following compiler error:
test.cpp: In function `int main()':
test.cpp:27: error: no matching function for call to `itoa_ex(int)'
even after changing the above code to:
template<int _OutSize, typename _String, typename _StringStream> typename _StringStream::_Mystr itoa_ex( int _Value ) { _StringStream Stream; Stream << _Value << std::endl; _String Str; Str = Stream.str(); typename _String::size_type Size; Size = Str.size() - 1; if( Size < _OutSize ) { Str.insert(0, "00000000000000000000000000000", _OutSize - Size ); } return Str; }#include <iostream>#include <string>#include <sstream>int main(){ std::cout << itoa_ex<12, std::string, std::stringstream>( 10 ).c_str() << std::endl; std::wcout << itoa_ex<12, std::string, std::wstringstream>( 10 ).c_str() << std::endl;}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement