C++ Weekly Episode 78: Intro to CMake—Jason Turner
Episode 78 of C++ Weekly.
Intro to CMake
by Jason Turner
About the show:
In this episode Jason gives an overview on how to use CMake for crossplatform C++ development.
September 13-19, Aurora, CO, USA
October 25, Pavia, Italy
November 6-8, Berlin, Germany
November 3-8, Kona, HI, USA
By Jason Turner | Aug 30, 2017 01:23 PM | Tags: basics
Episode 78 of C++ Weekly.
Intro to CMake
by Jason Turner
About the show:
In this episode Jason gives an overview on how to use CMake for crossplatform C++ development.
By Adrien Hamelin | Aug 30, 2017 12:45 PM | Tags: intermediate c++11
Quick A: Use integer_sequence with a helper function.
Recently on SO:
Array Initialisation Compile Time - Constexpr Sequence
1) How to implement that kind of integer_sequence?
template <std::size_t... Is> constexpr auto make_sequence_impl(std::index_sequence<Is...>) { return std::index_sequence<generate_ith_number(Is)...>{}; } template <std::size_t N> constexpr auto make_sequence() { return make_sequence_impl(std::make_index_sequence<N>{}); }2) Is it possible to build an std::array from that integer_sequence at compile time?
template <std::size_t... Is> constexpr auto make_array_from_sequence_impl(std::index_sequence<Is...>) { return std::array<std::size_t, sizeof...(Is)>{Is...}; } template <typename Seq> constexpr auto make_array_from_sequence(Seq) { return make_array_from_sequence_impl(Seq{}); }Usage:
int main() { constexpr auto arr = make_array_from_sequence(make_sequence<6>()); static_assert(arr[0] == 0); static_assert(arr[1] == 1); static_assert(arr[2] == 2); static_assert(arr[3] == 4); static_assert(arr[4] == 5); static_assert(arr[5] == 7); }
By Adrien Hamelin | Aug 30, 2017 12:34 PM | Tags: None
Quick A: The constructor needs an extra {}
pair.
Recently on SO:
vector does not convert brace encloser list
You have two options:
- add a constructor taking
std::initializer_list<std::initializer_list<T>>
- eclose the init expression with another set of
{}
i.e.Matrix<double> a{{ { 17, 24, 1}, { 23, 5, 7 }, { 4, 6, 13 } }};Ok, I'll try a little explanation of what is going on here:
If there is no constructor taking a
std::initializer_list
then the outermost{}
are always opening and closing the constructor call if you will, and not part of what you actually pass to the constructor.Matrix<double> a{ {1, 2}, {3, 4} }; ^ ^~~~~~~~~~~~~~ ^ | 2 parameters | | | | | opening closingAs you can see this is taken as a constructor with 2 parameters, in this case 2
initializer_lists
.This is why you need another set of
{}
:Matrix<double> a{ {{1, 2}, {3, 4}} }; ^ ^~~~~~~~~~~~~~~~ ^ | 1 parameter | | | | | opening closingIn order for the outermost
{}
to be considered aninitializer_list
then the constructor needs to have an overload taking ainitializer_list
. That is what is happening in thestd::vector
case.
By Adrien Hamelin | Aug 30, 2017 12:29 PM | Tags: intermediate
Have you registered for CppCon 2017 in September? Don’t delay – Registration is open now.
While we wait for this year’s event, we’re featuring videos of some of the 100+ talks from CppCon 2016 for you to enjoy. Here is today’s feature:
The Exception Situation
by Patrice Roy
Summary of the talk:
Exceptions have been a part of C++ for a long time now, and they are not going away. They allow programmers to concentrate on the meaningful parts of their code and treat the things that happen infrequently as… well, exceptional situations, to be dealt with when and where the context makes it reasonable or useful.
On the other hand, some significant parts of the C++ programming community either dislike this mechanism or outright reject it, for a number of reasons. Work in SG14 has raised performance issues in some cases; there are those who dislike the additional execution paths introduced in programs that rely on exceptions; some programmers raised issues with respect to exceptions and tooling, integration with older codebases, writing robust generic code, etc.
This talk will be neither for not against exceptions. It will present a perspective on cases where they make sense, cases where they are less appropriate, alternative disappointment handling techniques presented along with client code in order to show how the various approaches influence the way code is written. Performance measurements will be given along the way. Some creative uses of exceptions will also be presented in order to spark ideas and discussions in the room.
By Andrey Karpov | Aug 30, 2017 02:36 AM | Tags: tools static code analyzer pvs-studio devops
In this version there are improvements, which, in my opinion, deserve a small note.
Useful Improvements in the PVS-Studio 6.17 Release
by Andrey Karpov
From the article:
A much more interesting feature is that a mechanism of virtual values was significantly redesigned in the kernel of C++ analyzer. For example, now the analyzer performs a double loop passage, which allows it to define the range of possible values of variables, changing in a loop, more accurately. So don't be surprised if the analyzer starts issuing many warnings for that code which used to seem correct for the analyzer.
By Adrien Hamelin | Aug 29, 2017 12:57 PM | Tags: experimental community
Did you miss something?
C++ Annotated: Apr – Aug 2017
by Anastasia Kazakova
From the article:
Today we are happy to share our next compilation of C++ news with you.
By Adrien Hamelin | Aug 29, 2017 12:54 PM | Tags: community
Who wants to participate?
Calls for Lightning Talks and Open Content
From the article:
Less that 30 days out from CppCon 2017, regular session and poster submissions are closed, both of the field trip tours are sold out, and most of our official hotel blocks are either closed or sold out.
But, even now, there are still conference opportunities. There is still over two weeks left of regular registration, we are still accepting class registrations, we have rooms available in some of our official hotel blocks, and it isn’t too late to attend sessions for free by signing up as a volunteer.
To day we are also opening up two ways to present at the conference.
By Adrien Hamelin | Aug 29, 2017 12:43 PM | Tags: intermediate c++17
A handy new feature:
C++17: Inline Variables
by Marc Gregoire
From the article:
Before C++17, if your class had any non-const static data members, you had to allocate memory for them.
By Adrien Hamelin | Aug 28, 2017 01:25 PM | Tags: community
How to use Visual:
Visual C++ for Linux Development with CMake
by Marc Goodner
From the article:
In Visual Studio 2017 15.4 you can now target Linux from your CMake projects. This enables you to work on your existing code base that uses CMake as your build solution without having to convert it to a VS project. If your code base is cross-platform you can target both Windows and Linux from within Visual Studio.
This post will give an overview of the CMake support for Linux in Visual Studio. You can go here to learn more about CMake in Visual Studio generally.
By Adrien Hamelin | Aug 28, 2017 01:09 PM | Tags: efficiency c++11
You use threads? You should know this.
Top 20 C++ multithreading mistakes and how to avoid them
by Deb Haldar
From the article:
Threading is one of the most complicated things to get right in programming, especially in C++. I've made a number of mistakes myself over the years. Most of these mistakes were luckily caught in code review and testing ; however, some arcane ones did slip through and make it into production code and we had to patch live systems, which is always expensive.
In this article, I've tried to catalog all the mistakes I know of, with potential solutions. If you know any more pitfalls, or have alternative suggestions for some of the mistakes – please leave a comment below and I'll factor them into the article.