Forum Replies Created

Viewing 15 posts - 316 through 330 (of 344 total)
  • Author
    Posts
  • in reply to: Data generated at compile time #10051
    RainerRainer
    Keymaster
        Up
        0
        Down
        ::

        Here is a valid example:

        #include <algorithm>
        #include <iostream>
        #include <vector>
        
        constexpr int maxElement() {
            std::vector myVec = {1, 2, 4, 3};
            std::sort(myVec.begin(), myVec.end());
            return myVec.back();
        }
        int main() {
        
            std::cout <<  '\n';
        
            constexpr int maxValue = maxElement();
            std::cout << "maxValue: " << maxValue << '\n';
        
            std::cout << '\n';
        
        }

        As you can see from the Compiler Explorer output (https://godbolt.org/z/bcsWrbG85), only the maxElement is left from the vector.

        in reply to: Ordered List are not shown correctly #10050
        RainerRainer
        Keymaster
            Up
            0
            Down
            ::

            Now, it should work. I overwrote the css configuation of my theme for bbpress.

            in reply to: Initialization Exercise: another implementation #10029
            RainerRainer
            Keymaster
                Up
                0
                Down
                ::

                Since C++17, the C++ compiler  can automatically deduce the template arguments for class templates:

                std::vector<int> myVec{1, 2, 3};

                becomes, therefore:

                std::vector myVec{1, 2, 3};
                in reply to: auto versus decltype #9993
                RainerRainer
                Keymaster
                    Up
                    1
                    Down
                    ::

                    auto and decltype serve similar purposes: deduce the type of an expression. auto let you declare a variable with a specific type, and decltype extracts the type of a variable.

                    • auto (auto i = 5)
                      • The type of a expression will be deduced from an initializer
                      • Avoids long verbose type specification when defining a variable
                      • auto type deduction is essentially template type deduction
                      • Returns the decayed type (https://en.cppreference.com/w/cpp/types/decay)
                    • decltype (decltype(5))
                      • Determines the declared type of an entitiy or an expression
                      • Returns the exact type

                    I consider auto to be a purely simplifying feature whereas the primary purpose of decltype is to enable sophisticated metaprogramming in foundation libraries. They are however very closely related when looked at from a language-technical point of use. (From HOPL20 4.2.1, Bjarne Stroustrup).

                     

                    in reply to: Overusing auto #9971
                    RainerRainer
                    Keymaster
                        Up
                        0
                        Down
                        ::
                        auto firstVar = 1.2; // double
                        
                        auto secondVar = 1.2f // float
                        

                        The compiler deduces the type of the initializer. In the first case, 1.2 is a double literal; in the second case, 1.2f is a float literal. Therefore, firstVar becomes a double, and secondVar becomes a float. I see no issue with type promotion.

                         

                        in reply to: Assembly Basics #9966
                        RainerRainer
                        Keymaster
                            Up
                            0
                            Down
                            ::

                            Honestly, I’m not an assembler expert, but my knowledge is most of the time sufficient to study the output of the Compiler Explorer. My assembler experience goes back into the 90ths and is based on a predecessor of the Motorola 68000 family.

                            Most of the time, there is one book suggested, when it comes to a deeper understanding of assembler: the dragon book. Have a look her: https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools.

                            in reply to: Initialization of Raw Strings Literals #9963
                            RainerRainer
                            Keymaster
                                Up
                                0
                                Down
                                ::

                                There is no difference. The compiler explorer proofs it. Don’t use C++ Insights for your analysis. C++ Insights just show the first transformations of your compiler, before any opimization kicks in.

                                in reply to: Specifying the used C++ Standard #9961
                                RainerRainer
                                Keymaster
                                    Up
                                    0
                                    Down
                                    ::

                                    The answer depends on the version of your compiler. Each compiler uses a default C++ standard for code generation.

                                    • GCC

                                    For gcc, the following holds:

                                    • 5.5.0: C++11 is the default for code generation
                                    • 6.1.0: C++14 is the default for code generation
                                    • 11.1.0: C++17 is the default for code generation

                                    When you want to use C++20 with the g++ Version 11.1.0, you have to specify the standard: g++ -std=c++20.

                                    • Clang

                                    The gcc flags work for clang++.

                                    • MSVC

                                    With the MSVC compiler, I always use the following flag: /std:c++latest, but you can also specify /std:c++11, /std:c++14, and so on.

                                    in reply to: Function by reference #9951
                                    RainerRainer
                                    Keymaster
                                        Up
                                        0
                                        Down
                                        ::

                                        So far, I have no use-case in mind. This example only serves as an example to show you the difference. The following line without the reference gives you a pointer.

                                        auto& myFuncRef = func;
                                        

                                         

                                         

                                        in reply to: Overusing auto #9949
                                        RainerRainer
                                        Keymaster
                                            Up
                                            0
                                            Down
                                            ::

                                            Very good question. Here are my thoughts.

                                            For performance and readability reasons, I prefer the following syntax.

                                            std::vector<int> myFunc() {
                                            
                                               return {1, 2, 3};
                                            
                                            }
                                            

                                            The return value is a temporary. Therefore, return value optimization (RVO, https://www.modernescpp.com/index.php/cpp17-core) kicks in, and the vector is directly created in the caller:

                                            std::vector<int> result = myFunc();

                                            Honestly, I’m not sure if I would use in this case auto or std::vector<int> for result.

                                            May decision depends on one point. Do I have a meaningful name for result or not? If yes, I prefer auto:

                                            auto userIDs = myFunc();
                                            

                                            The readability of your program should not be based on type information, but on good names. auto enforces this is some way. types have no semantic. A std::vector<int> can be anything.

                                            RainerRainer
                                            Keymaster
                                                Up
                                                0
                                                Down
                                                ::

                                                Thanks a lot. Fixed.

                                                in reply to: “Mark Complete” and watching ahead #9922
                                                RainerRainer
                                                Keymaster
                                                    Up
                                                    0
                                                    Down
                                                    ::

                                                    I hope, I get you right. You want a kind of tag system. You tag all lessons that are worth to watch once more. Let me see what I can find.

                                                    in reply to: “Mark Complete” and watching ahead #9917
                                                    RainerRainer
                                                    Keymaster
                                                        Up
                                                        0
                                                        Down
                                                        ::

                                                        I mainly enabled “Marked Complete” because I don’t want to produce all videos before the mentoring.

                                                        Now, I’m almost done and I, therefore disabled, “Marked Complete”. But keep in mind, I’m almost done. There are still improvement I will make for the upcoming weeks.

                                                        I don’t know what you mean with “to review”. Please elaborate.

                                                        in reply to: lvalue/rvalue references #9808
                                                        RainerRainer
                                                        Keymaster
                                                            Up
                                                            0
                                                            Down
                                                            ::

                                                            I explain in detail lvalue reference and rvalue references when we talk about move semantics and perfect forwarding. This is week 6.
                                                            In this example I used various lvalues and rvalues and invoked a function template is_lvalue, overloaded for a lvalue reference and a rvalue reference: https://godbolt.org/z/hbWGznhsq
                                                            I discuss this in our next Q&A session.

                                                            RainerRainer
                                                            Keymaster
                                                                Up
                                                                0
                                                                Down
                                                                ::

                                                                I could not find any wording about the behavior in the working draft of the C++ standard: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/n4910.pdf

                                                                I could also not find anything in the original proposal: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf

                                                                The MSVC only diagnoses a warning, but gcc, clang, and icc (Intel) an error: https://godbolt.org/z/z7qvEeGfb

                                                                The practice is that we get at least a warning.

                                                              Viewing 15 posts - 316 through 330 (of 344 total)