Dealing with Unicode and ANSI - Templates

This is a discussion on Dealing with Unicode and ANSI - Templates within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  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.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,259
    hmm... why can you not use templates?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,259
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  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??

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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?

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    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,893
    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, 09:06 AM
  2. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 01:48 AM
  3. Unicode v ANSI Calls
    By Davros in forum Windows Programming
    Replies: 3
    Last Post: 04-18-2006, 10: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, 08:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21