Forum Replies Created

Viewing 15 posts - 256 through 270 (of 349 total)
  • Author
    Posts
  • RainerRainer
    Keymaster
        Up
        0
        Down
        ::

        I implemented readData based on a technique called dispatch table: https://godbolt.org/z/KW9MWr98e

        in reply to: Video on week 17 template specialization – introspection #86434
        RainerRainer
        Keymaster
            Up
            0
            Down
            ::

            Thank you. I fixed it.

            in reply to: Forum post not listed #85052
            RainerRainer
            Keymaster
                Up
                1
                Down
                ::

                I found the issue. When you have two or more links in your topic, WordPress regards this as spam and I, therefore, have to approve. I increased the limit to 5.

                in reply to: Hash function for unordered_map #85051
                RainerRainer
                Keymaster
                    Up
                    1
                    Down
                    ::

                    A good starting point for a hash function is in general is to use bit shifting and bitwise XOR to combine the individual hash values. I assume, boost applies this idea.
                    You can check how good your hash function is and visualize the distribution of the keys: The following lesson should give you the idea:  https://www.modernescpp.org/topics/example-unorderedsethashinfo-cpp/

                    in reply to: Forum post not listed #85050
                    RainerRainer
                    Keymaster
                        Up
                        0
                        Down
                        ::

                        Honestly, I don’t know why this happens. In the documentation, they say that your topic is pending if you post the first time. Of course, this is not true for you. I will investigate more.

                        RainerRainer
                        Keymaster
                            Up
                            0
                            Down
                            ::

                            The historical reason is the following:

                            Because string literals are objects with internal linkage (two string literals with the same value but in different modules are different objects), you can’t use them as template arguments either. (C++ Templates by Addison Wesley)

                            This means, that two identical strings such as “hello” can have different addresses in different translation units.

                            #include <iostream>
                            
                            template <const char* str>
                            void func() { std::cout << str << '\n'; }
                            
                            int main() {
                                func<"hello">();
                            }
                            

                            ⇒   An additional instantiation of func<“hello”>() in another translation unit may be different.

                            in reply to: Compile time functions #81654
                            RainerRainer
                            Keymaster
                                Up
                                0
                                Down
                                ::

                                Usually, you use the type-traits functions in a constexpr context. Here are a few examples:

                                1. static_assert
                                2. implementation of a concept in C++20
                                3. compile time decision

                                But a type-traits function can also be used at run time: https://godbolt.org/z/hjvqT46TP

                                This means, a type-traits function must run at compile time:

                                • When used in a constexpr context (see the three points above)
                                • When taken by a constexpr variable: constexpr auto res = std::is_integral<decltype(a)>::value;

                                C++20:

                                • You can use a type-traits function in a consteval function. A consteval function can only run at compile time.
                                • The new function is_constant_evaluated() if a constexpr function runs at compile time or run time. Watch my CppCon 2021 presentation: Back to Basics: const and constexpr
                                RainerRainer
                                Keymaster
                                    Up
                                    0
                                    Down
                                    ::

                                    With C++20, templates can and should be in modules. Modules have a unique internal representation that is neither source code nor assembly. This representation is a kind of abstract syntax tree (AST). Thanks to this AST, the template definition is available during template instantiation.

                                    You can read more details here: https://www.modernescpp.com/index.php/c-20-open-questions-to-modules

                                    in reply to: C++ Insights #81161
                                    RainerRainer
                                    Keymaster
                                        Up
                                        1
                                        Down
                                        ::

                                        Here is the answer from Andreas Fertig, creator of C++ Insights: “Jemand anders hat ein Plugin für VS-Code geschrieben, dass C++ Insights nutzt: https://github.com/andreasfertig/cppinsights/#c-insights–vscode” (Someone else wrote a plugin for VS-Code that uses C++ Insights: https://github.com/andreasfertig/cppinsights/#c-insights–vscode)

                                         

                                         

                                        in reply to: non-type parameters #80749
                                        RainerRainer
                                        Keymaster
                                            Up
                                            0
                                            Down
                                            ::

                                            std::vector has only a type template parameter. Therefore, it is not an example of a non-type template parameter.

                                            in reply to: Inheriting from bank account #80349
                                            RainerRainer
                                            Keymaster
                                                Up
                                                0
                                                Down
                                                ::

                                                Exactly. I mean Account::withdraw(amt). Here is the fix: https://godbolt.org/z/hTeEcqGo9

                                                in reply to: Using types to do branching #80079
                                                RainerRainer
                                                Keymaster
                                                    Up
                                                    0
                                                    Down
                                                    ::

                                                    This is function overloading. The second overload of fill_impl is chosen if boolType() evaluates to std::true_type.

                                                    in reply to: Virtual Destructor #78959
                                                    RainerRainer
                                                    Keymaster
                                                        Up
                                                        0
                                                        Down
                                                        ::

                                                        Your example is broken. It has undefined behavior. Undefined behavior mean that not guarantees are provided by the C++ standard. The only thing you can do is to fix the issue.

                                                        in reply to: Virtual Destructor #78945
                                                        RainerRainer
                                                        Keymaster
                                                            Up
                                                            0
                                                            Down
                                                            ::

                                                            Your Account does not have a virtual destructor. Destructing BankAccount with the interface of Account is, therefore, undefined behavior. When you make the destructor of Account virtual, your program works as expected: https://godbolt.org/z/fP4cxezMP

                                                            RainerRainer
                                                            Keymaster
                                                                Up
                                                                0
                                                                Down
                                                                ::

                                                                Honestly, I’m not sure if I like this example from the C++ Core Guidelines. At least, here are my answers.

                                                                • Virtual inheritance overcomes the diamond problem: The diamond problem occurs when two super classes of a class Derived have a common base class Base. Consequentially, Derived has two subjects Base.  ⇒  This is an ambiguous error if class Derived uses functionality of class Base.  When both super classes of Derived are inherited virtually, Derived will only have on subobject Base.  Read more here: https://isocpp.org/wiki/faq/multiple-inheritance#mi-diamond
                                                                • move is critical as a freestanding function.
                                                                  • You may have to define it on each interface (Smiley, Circle, and Shape)
                                                                  • When you have more than one implementation, you mix function overloading with inheritance. ⇒ Each interface requires its overload of move.
                                                                  • It may conflict with std::move if someone uses a using declaration: using std::move.
                                                                • There is no need for a friend because the pure interface Shape has nothing to protect. It has no state.

                                                                Intermixing inheritance with function overloading looks strange:

                                                                #include <iostream>
                                                                
                                                                struct Base {
                                                                    virtual void test() {
                                                                        std::cout << "Base\n";
                                                                    }
                                                                };
                                                                
                                                                struct Derived: Base {
                                                                    void test() override {
                                                                        std::cout << "Derived";
                                                                    }
                                                                };
                                                                
                                                                void move(Base* b) {
                                                                    b->test();
                                                                }
                                                                
                                                                void move(Derived* d) {
                                                                    d->test();
                                                                }
                                                                
                                                                int main() {
                                                                
                                                                    Base b;
                                                                    Derived d;
                                                                
                                                                    move(&b);
                                                                    move(&d);
                                                                
                                                                }
                                                                
                                                              Viewing 15 posts - 256 through 270 (of 349 total)