Forum Replies Created
-
AuthorPosts
-
18. September 2022 at 06:47 in reply to: Class with unique_ptr member variables discussed in Q&A session #86976::
I implemented readData based on a technique called dispatch table: https://godbolt.org/z/KW9MWr98e
16. September 2022 at 06:47 in reply to: Video on week 17 template specialization – introspection #86434::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/6. September 2022 at 07:16 in reply to: Strings cannot be used as non-type template parameters. But why? #81814::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.
::Usually, you use the type-traits functions in a constexpr context. Here are a few examples:
- static_assert
- implementation of a concept in C++20
- 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
5. September 2022 at 21:36 in reply to: Can we define and implement the methods of a class template in cpp file? #81653::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
::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)
::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
26. August 2022 at 15:36 in reply to: C129: distinguish between implementation inheritance and interface inheritance #78657::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); }
-
AuthorPosts
