Thread: Dealing with Unicode and ANSI - Templates

  1. #1
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

    Dealing with Unicode and ANSI - Templates

    I'm chronically experiencing a problem while trying to deal with both unicode and ansi conditionally. Code

    http://zxcvbn.googlecode.com/svn/trunk/pla...nesplaylist.cpp

    You'll see, I deal with both the ANSI and Unicode cases exactly the same

    Code:
    	if(unicode)
    	{
    		wfilestream.open(filename.c_str());
    		fileok = !!wfilestream;
    	}
    	else
    	{
    		filestream.open(filename.c_str());
    		fileok = !!filestream;
    	}
    
    	if(!fileok)
    	{
    		std::cout << "File not opened\n";
    		return 0;
    	}
    
    
    	std::string line;
    	std::wstring wline;
    
    	bool fok;
    
    	if(unicode)
    		fok = !!std::getline(wfilestream, wline);
    	else
    		fok = !!std::getline(filestream, line);
    
    ... etc ...
    But because the templates are a compile time deal, I have had to check for both at the same time. I am not aware of a polymorphic base type that would allow me to do this more generally, is there any standards compliant sort of thing? Is there a way to do run-time template specialization? This just seems outrageous and I can't see how to go about it correctly.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... why can you not use templates?
    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 Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I do use templates. But it's like, I determine at runtime whether I'm going to be using a unicode stream or an ansi one, I can't just have it specialize the template then eh? I guess I could just compile two separate executables, one for reading ansi and one for unicode, but, uh, that's weird in my mind.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I determine at runtime whether I'm going to be using a unicode stream or an ansi one, I can't just have it specialize the template then eh?
    Of course, but you could have function templates that do the file handling for you, and then after you have decided on whether unicode should be used you invoke the appropriate one (possibly with a char or wchar_t). The bad part is that template bloat will mean that you still have two sets of the same code, though you only need maintain one set.
    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 Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Yeah, I dunno why I couldn't get my head around that. Mmhmm. I've still done stuff like

    Code:
                    if((typeid(T) == typeid(wchar_t) && *i == L'\t') || 
                        (typeid(T) == typeid(char) && *i == '\t'))
                    {
                        element += *i;
                    }
    But yeah, all is going much better now.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Revisiting this, I amk still stumbling over what I alluded to in my last post.

    Code:
    template <typename T> void foo()
    {
    	if(typeid(T) == typeid(wchar_t))
    		lol = L"Blah blah";
    	else if(typeid(T) == typeid(char))
    		lol = "Blah blah";
    }
    I instantiate (proiper term when it's a function) both wchar_t and char instances (proper term when it's a function?) of this template, so it gives me errors like

    Code:
    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const char [5]' (or there is no acceptable conversion)
            could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)'
            with
            [
                _Elem=wchar_t,
                _Traits=std::char_traits<wchar_t>,
                _Ax=std::allocator<wchar_t>
            ]
            or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
            with
            [
                _Elem=wchar_t,
                _Traits=std::char_traits<wchar_t>,
                _Ax=std::allocator<wchar_t>
            ]
            or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
            with
            [
                _Elem=wchar_t,
                _Traits=std::char_traits<wchar_t>,
                _Ax=std::allocator<wchar_t>
            ]
            while trying to match the argument list '(std::basic_string<_Elem,_Traits,_Ax>, const char [5])'
            with
            [
                _Elem=wchar_t,
                _Traits=std::char_traits<wchar_t>,
                _Ax=std::allocator<wchar_t>
            ]
    see reference to function template instantiation 'int writeM3U<wchar_t>(const std::string &,const table<T> &)' being compiled
            with
            [
                T=wchar_t
            ]
    error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const char [5]' (or there is no acceptable conversion)
            could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)'
            with
            [
                _Elem=wchar_t,
                _Traits=std::char_traits<wchar_t>,
                _Ax=std::allocator<wchar_t>
            ]
            or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
            with
            [
                _Elem=wchar_t,
                _Traits=std::char_traits<wchar_t>,
                _Ax=std::allocator<wchar_t>
            ]
            or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
            with
            [
                _Elem=wchar_t,
                _Traits=std::char_traits<wchar_t>,
                _Ax=std::allocator<wchar_t>
            ]
            while trying to match the argument list '(std::basic_string<_Elem,_Traits,_Ax>, const char [5])'
            with
            [
                _Elem=wchar_t,
                _Traits=std::char_traits<wchar_t>,
                _Ax=std::allocator<wchar_t>
            ]
    What might I do to maybe, use not run-time type identification, but compile time, at a more preprocessor like level, to stop these errors??

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm just throwing this out there, not sure if it would work or if it is relevant.

    Perhaps you should make the template type the character, and then use the streams and strings with their long names (like basic_string<T, std::char_traits<T>, std::alocator<T>).

    For things like the L"Blah blah" versus "Blah blah", you could have a template specialization just for that piece of code.

    If you want to go the preprocessor route, I'd imagine something like Microsoft's _T macros and _t functions might help.

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> For things like the L"Blah blah" versus "Blah blah", you could have a template specialization just for that piece of code.

    I believe this is what I want. How might I do this?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Something like:
    Code:
    template <typename T> void foo()
    {
      // cause a compile time error, I forget exactly how
    }
    
    template <> void foo<wchar_t>()
    {
      lol = L"Blah blah";
    }
    template <> void foo<char>()
    {
      lol = "Blah blah";
    }
    
    template <typename T> void bar()
    {
      // do some stuff
      foo<T>();
      // use lol.
    }

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But it's like, I determine at runtime whether I'm going to be using a unicode stream or an ansi one,
    In my opinion, your problem is right here. Why do you do this at runtime?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ANSI v.s. Wide
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-20-2008, 08:06 AM
  2. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  3. Unicode v ANSI Calls
    By Davros in forum Windows Programming
    Replies: 3
    Last Post: 04-18-2006, 09:35 AM
  4. Turn off unicode in MSVC++ Express 2005
    By Quantum1024 in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2005, 04:59 PM
  5. Visual C++ and UNICODE - _t macros
    By nvoigt in forum Windows Programming
    Replies: 2
    Last Post: 04-22-2005, 07:42 AM