Thread: using facet to covert from encoding to encoding

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    using facet to covert from encoding to encoding

    using the example from cppreference:
    std::codecvt - cppreference.com

    I've managed to convert from utf-8 to utf-32. here is my code:

    Code:
    template<class Facet>
    struct deletable_facet : Facet
    {
        template<class ...Args>
        deletable_facet(Args&& ...args) : Facet(std::forward<Args>(args)...) {}
        ~deletable_facet() {}
    };
    
    int main(){
    
        std::string data = "hello world";
    
        std::wstring_convert<deletable_facet<std::codecvt<char32_t, char, std::mbstate_t>>, char32_t> toUTF32Convertor;
        std::u32string str32 = toUTF32Convertor.from_bytes(data);
    }
    it works with this example, but I wanted to ask few questions:
    1) is this code valid? it works for "hello world" example, yes, but I wonder if it's a one-time charm or is it actually functioning like it should.
    2) I don't get the whole "inherit from unknown Facet class and move all the arguments to the base constructor"
    3) I don't get the whole "inherit from std::codecvt", why do we need to inherit from coedcvt at the first place?
    (4- why couldn't we get some nicer API :-) ?)

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    is this code valid?
    O_o

    Yes. Strangely, the code would be valid even if the resulting executable didn't work.

    it works for "hello world" example, yes, but I wonder if it's a one-time charm or is it actually functioning like it should.
    The "it works" bit is a quality of implementation issue.

    A shipping implementation of the standard library just doesn't have to provide the relevant `std::codecvt<???>` specialization.

    I don't get the whole "inherit from unknown Facet class and move all the arguments to the base constructor"
    I don't get the whole "inherit from std::codecvt", why do we need to inherit from coedcvt at the first place?
    I don't know why you really just asked the same question twice.

    Why wouldn't you use a standard mechanism for the conversion? The use of standard components explains why you are using the `std::codecvt<???>` template class, and the use of idiomatic C++ techniques within the fledgling C++ standard library explains the wrapper. The `std::codecvt<???>` template class was designed to be used in specific circumstances essentially protecting the class by preventing a client from trying certain bits of nonsense. The derived class really just exposes the destructor for use by a client.

    why couldn't we get some nicer API
    You could always write convenience interfaces over the library components, but you can't really add functionality to an implementation unless the interfaces are capable of expressing the extra functionality.

    People who ask for simpler localization interfaces usually don't understand the complexity inherent in localization.

    The fact is that, despite the richness of the offered components, the standard library isn't really sophisticated enough to handle some real world requirements.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    Quote Originally Posted by phantomotap View Post
    O_o

    Yes. Strangely, the code would be valid even if the resulting executable didn't work.

    The "it works" bit is a quality of implementation issue.

    A shipping implementation of the standard library just doesn't have to provide the relevant `std::codecvt<???>` specialization.
    I don't understand your answer. will the code convert correctly from utf8 to utf32 (assuming you put valid utf8 strings to it)?

    Quote Originally Posted by phantomotap View Post
    I don't know why you really just asked the same question twice.

    Why wouldn't you use a standard mechanism for the conversion? The use of standard components explains why you are using the `std::codecvt<???>` template class, and the use of idiomatic C++ techniques within the fledgling C++ standard library explains the wrapper. The `std::codecvt<???>` template class was designed to be used in specific circumstances essentially protecting the class by preventing a client from trying certain bits of nonsense. The derived class really just exposes the destructor for use by a client.
    they are not the same question. the first question is - what benefit do we get from inheriting and not adding any functionallity?

    Code:
    class A{int x; }; class B : public A{};
    what does B gives us that A doesn't? why did we needed this step in the first place? this is just strange that someone decided to inherit from one class, add no functionality and put it as a valid argument to a standard class .

    the second question is : where in the documentation is it said you should put anything other then std::codecvt as template argument?

    Quote Originally Posted by phantomotap View Post
    You could always write convenience interfaces over the library components, but you can't really add functionality to an implementation unless the interfaces are capable of expressing the extra functionality.

    People who ask for simpler localization interfaces usually don't understand the complexity inherent in localization.

    The fact is that, despite the richness of the offered components, the standard library isn't really sophisticated enough to handle some real world requirements.

    Soma
    can you allaborate on this?

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    can you allaborate on this?
    O_o

    Nope. I'm drained. I'm tired of getting foolish responses like "what benefit do we get from inheriting and not adding any functionallity?" and "what does B gives us that A doesn't?" to my posts. I'm tired of repeating myself just because people like you don't put any effort into understanding comments due to a desire for reinforcement of flawed preconceptions.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    ok.
    Anyone else want to join and explain?

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> why do we need to inherit from coedcvt at the first place?
    Because the facet template being passed to std::wstring_convert<> needs to be constructable. std::codecvt<> is not constructable (because it has a protected destructor).

    >> why couldn't we get some nicer API
    I guess they thought that codecvt_utf8_utf16<> was good enough and left off codecvt_utf8_utf32<>.

    gg

  7. #7
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    thanks you both.
    I get it now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone know about J-bit encoding?
    By MutantJohn in forum C Programming
    Replies: 18
    Last Post: 06-24-2013, 05:53 PM
  2. Encoding a data structure based on TLV encoding
    By Sajas K K in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2013, 10:39 PM
  3. How to convert string in url encoding to html encoding?
    By Jerel2k11 in forum C Programming
    Replies: 6
    Last Post: 11-06-2011, 09:05 AM
  4. URL Encoding
    By nickname_changed in forum C# Programming
    Replies: 2
    Last Post: 03-13-2004, 03:22 AM
  5. Encoding
    By gvector1 in forum C# Programming
    Replies: 0
    Last Post: 06-20-2003, 10:17 AM