Forum Replies Created
-
AuthorPosts
-
::
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.
::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).
::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.
::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.
::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.
::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.
17. May 2022 at 22:07 in reply to: autoExplicit.cpp in root folder is the same as in solutions folder #9924::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.
::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.11. May 2022 at 08:42 in reply to: Scoped enum overflow: Different behaviour between MSVC and GCC (std=c++17) #9754::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.
- auto (auto i = 5)
-
AuthorPosts