Thread: template function

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    330

    template function

    I have 2 functions here that do almost the same thing. One converts a string to wstring and the other wstring to string. Somehow I think I should create one template function for this but dont see how to do it. Anyone else have an idea on how to do this?

    Code:
    static std::string wide_to_char(const std::wstring &ws)
    {
    	std::string s;
    
    	for (std::string::size_type i = 0; i < ws.length(); i++)
    	{
    		char c;
    		wctomb(&c, ws[i]);
    
    		s.push_back(c);
    	}
    
    	return s;
    }
    
    static std::wstring char_to_wide(const std::string &s)
    {
    	std::wstring ws;
    
    	for (std::string::size_type i = 0; i < s.length(); i++)
    	{
    		wchar_t c;
    		mbtowc(&c, &s[i], 1);
    
    		ws.push_back(c);
    	}
    
    	return ws;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I do not think you can write a single template function for this as the usage patterns of wctomb and mbtowc are slightly different, so it is not just a matter of passing a function as an argument to be applied.

    But, if you write wrappers for wctomb and mbtowc, you can then pass the appropriate wrapper function (or function object) as an argument to std::transform, instead of writing the loops by hand (and of course, you would then have increased flexibility, and retain the option of writing convenience functions that make use of these std::transform calls).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    yeah probably. I came up with this but I think its more complex than just write 2 functions. Was hoping for a shorter/easier solution

    Code:
    static char exec(wchar_t from) 
    { 
      char c;
      wctomb(&c, from);
      return c;
    }
    
    static wchar_t exec(char from)
    {
      wchar_t c;
      mbtowc(&c, &from, 1);
    
      return c;
    }
    
    template<typename To, typename From>
    To convert(const From &s)
    {
    	To out;
    
    	for (From::size_type i = 0; i < s.length(); i++)
    	{
    		out.push_back(exec(s[i]));
    	}
    
    	return out;
    }
    Last edited by KIBO; 08-09-2010 at 03:27 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Actually, I had this in mind:
    Code:
    char wide_to_char(wchar_t wc)
    {
        char c;
        wctomb(&c, wc);
        return c;
    }
    
    wchar_t char_to_wide(char c)
    {
        wchar_t wc;
        mbtowc(&wc, &c, 1);
        return wc;
    }
    
    std::string wide_to_string(const std::wstring& ws)
    {
        using namespace std;
        string s;
        transform(ws.begin(), ws.end(), back_inserter(s), wide_to_char);
        return s;
    }
    
    std::wstring string_to_wide(const std::string& s)
    {
        using namespace std;
        wstring ws;
        transform(s.begin(), s.end(), back_inserter(ws), char_to_wide);
        return ws;
    }
    If you prefer, the latter two convenience functions could be replaced by a convenience function template:
    Code:
    template<typename Dest, typename Source, typename Func>
    Dest convert(const Source& source, Func func)
    {
        using namespace std;
        Dest dest;
        transform(source.begin(), source.end(), back_inserter(dest), func);
        return dest;
    }
    which you can then call such as:
    Code:
    std::wstring ws;
    // ...
    std::string s = convert<std::string>(ws, char_to_wide);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    yeah thats a nice solution. thanks!

  6. #6
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Only if your wide chars don't actually require multiple chars to store, otherwise wctomb will overflow the given buffer and mbtowc will convert garbage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM