Forum Replies Created

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • in reply to: Constexpr of std::string objects #147087
    AvatarFabio Lopez
    Participant
        Up
        0
        Down
        ::

        I found your question interesting, so I threw together a quick and dirty experiment. Link: https://godbolt.org/z/4WP4PTPnE
        These are my findings.

        I would like an explanation as to why we can’t initialize a constexpr std::string_view with an std::string, but we can initialize it with a literal.

         1
         2
         3
         4
         5
         6
         7
         8
         9
        10
        11
        12
        13
        14
        #include <iostream>
        #include <string>
        #include <string_view>
        
        int main()
        {
            //constexpr std::string myString{"Hello World!"}; //This throws an error!
        
            std::string myString{"Hello World!"};
            //constexpr std::string_view myStringView{myString};  //Throws an error!
            constexpr std::string_view myStringView{"Hello World!"}; //Amazingly, this works
        
            std::cout << myStringView << '\n';
        }
        
        in reply to: Function Hiding vs Overriding #142556
        AvatarFabio Lopez
        Participant
            Up
            0
            Down
            ::

            I am sorry if I was not being clear enough, but you answered my question.
            I understand it now.

            Also thank you for adding the remark about virtual and override!

            in reply to: enum class declaration #109013
            AvatarFabio Lopez
            Participant
                Up
                0
                Down
                ::

                I don’t understand what you mean by “no value is critical”.
                Would you give me a quick example about using std::optional? Seems like an interesting tool.

                Also, how do make your code look so pretty in your posts? I am trying to find the option to make it look like that, but I don’t see it.

                in reply to: enum class declaration #107806
                AvatarFabio Lopez
                Participant
                    Up
                    0
                    Down
                    ::

                    I am sorry about the confusion.
                    I was just playing around with the Qt framework which I enjoy because of how easy it makes it to create UIs.

                    Basically my question is this. (See attached image.)
                    When creating an enum class, is it better to create it as its own entity outside any other class and if so, should be it be contained on its own file? or do I create it as part of the class I will be using it with?

                    As for the dispatch table example, I read the article a while ago and it gave me an idea. What do you think of this?
                    The original example is from Professional C++ by Marc Gregory, a book mentioned in other conversations, but I wanted to experiment a bit, so this is the result.
                    Note that here records is not a class. It is a namespace.

                    #include <unordered_map>
                    #include <iostream>
                    #include "UserInterface.h"
                    #include "Input_Validation.h"
                    #include <functional>
                     
                    int main()
                    {
                    records::Database employee_db;
                     
                    while (true)
                    {
                    records::display_menu();
                    std::unordered_map<std::string, std::function<void(records::Database&)>> sampleCommands
                    {
                    {"hire", records::do_hire },
                    {"fire", records::do_fire },
                    {"promote", records::do_promote },
                    {"demote", records::do_demote },
                    {"list all", records::display_all },
                    {"list current", records::display_current },
                    {"list former", records::display_former },
                    {"quit", records::quit },
                    {"help", records::help }
                    };
                    std::string user_input = records::command_validation(sampleCommands);
                     
                    if (user_input.empty())
                    {
                    std::cout << "Invalid Command\n";
                    continue;
                    }
                    sampleCommands[user_input](employee_db);
                    }
                    return 0;
                    }

                    Here is how I am performing the command validation.

                    std::string command_validation(std::unordered_map<std::string, std::function<void(records::Database&)>>& inCommands)
                    {
                    std::string user_input{ 0 };
                    std::getline(std::cin, user_input);
                    auto it = inCommands.find(std::move(user_input));
                     
                    if (it == inCommands.end())
                    {
                    return {};//"Invalid command\n";
                    }
                    return it->first;
                    }

                    AvatarFabio Lopez
                    Participant
                        Up
                        1
                        Down
                        ::

                        In my experience the problem also is the area where you live.
                        For example, in my city absolutely every single company is looking to either a Python developer or a web developer because it is the trendy thing.

                        I have seen projects where C++ would be the correct choice of language, but because the manager happened to like Python, the project had to be built in Python.
                        Another thing, the lack of a reliable path to learn the language is also detrimental.

                        For example for languages like C# you have learn.coderfoundry.com and Iamtimcorey.com, they specialize in teaching the technology by using it in actual projects that you can publish in your own portfolio website and I am not talking about “Hello World” projects, but actual useful business applications. Once you have built a couple of apps with them, then you are encouraged to build your own personal project.

                        I rarely see something like that with C++, most books and training teach the specifics of the language and at most the give you some questions related to the actual topic, but that’s it.
                        There is no project that you build while learning the features.
                        Sure, you can come up with an idea and build your own thing on your own, but then there is the architecture, design and other things that a C++ book does not teach.
                        Tackling a project without guidance as a beginner is incredibly intimidating.

                        It is the “OK, this is a cool feature, but how do I use this in a real life project?” question that drives most people away from the language (In my opinion).

                        The other thing is that as mentioned, companies are refusing to pay or implement any kind of professional C++ training, so you are in a situation where you learn C++ from a couple of books and then
                        you join the workforce, but the codebase is so murky and specialized that not even those books or stackoverflow can help you out, yet you are expected to make something out of the mess you are inheriting,

                        I apologize if it sounds like I am complaining. I am just trying to list some of the frustrations that I and some people I know have when trying to learn C++ to be able to perform at a professional level.

                      Viewing 5 posts - 1 through 5 (of 5 total)