Jump to content
  • Advertisement
Sign in to follow this  

templates freak out on gcc...

This topic is 4726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This code freaks out on gcc, and I don't think I'm able to see why...
 	
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;
}

	



anyone?

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Try:

#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.

Share this post


Link to post
Share on other sites
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;
}


Share this post


Link to post
Share on other sites
thanks Washu, but I wanna know why, more than wanting it to work ... It stumbed me at first.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!