c++17

What are contracts (as proposed for C++17)?

谁都会走 提交于 2019-12-02 17:03:10
I was reading about contracts in Thoughts about C++17 by B. Stroustrup and assisted a small presentation talking about them but I am not sure I have understood them really. So I have a some interrogations and if it is possible to illustrate them with some examples : Are contracts just a better replacement of the classic assert() and should they be used together ? What contracts really are put in simple terms for a software dev ? Would contracts have an impact on how we handle exceptions ? If yes, how should we use exceptions and contracts ? Would using contracts imply an overhead at execution

Why use std::make_unique in C++17?

依然范特西╮ 提交于 2019-12-02 16:54:50
As far as I understand, C++14 introduced std::make_unique because, as a result of the parameter evaluation order not being specified, this was unsafe: f(std::unique_ptr<MyClass>(new MyClass(param)), g()); // Syntax A (Explanation: if the evaluation first allocates the memory for the raw pointer, then calls g() and an exception is thrown before the std::unique_ptr construction, then the memory is leaked.) Calling std::make_unique was a way to constrain the call order, thus making things safe: f(std::make_unique<MyClass>(param), g()); // Syntax B Since then, C++17 has clarified the evaluation

Overloading multiple function objects by reference

浪尽此生 提交于 2019-12-02 16:21:59
In C++17, it is trivial to implement an overload(fs...) function that, given any number of arguments fs... satisfying FunctionObject , returns a new function object that behaves like an overload of fs... . Example: template <typename... Ts> struct overloader : Ts... { template <typename... TArgs> overloader(TArgs&&... xs) : Ts{forward<TArgs>(xs)}... { } using Ts::operator()...; }; template <typename... Ts> auto overload(Ts&&... xs) { return overloader<decay_t<Ts>...>{forward<Ts>(xs)...}; } int main() { auto o = overload([](char){ cout << "CHAR"; }, [](int) { cout << "INT"; }); o('a'); //

constexpr defining static data member of literal type that is declared const

眉间皱痕 提交于 2019-12-02 15:08:49
问题 I have a question about constexpr defining a static data member of literal type that is declared const (and not specified inline or constexpr) in the class definition: // S.h struct S { static int const i; // not specified inline or constexpr }; // S.cpp #include "S.h" constexpr int const S::i = 42; // definition, not declaration // main.cpp #include "S.h" int main() { return S::i; } Clang/gcc return 42 in C++11/14 mode, but report an error (undefined reference to S::i) in C++17 mode. If I

Most elegant way to write a one-shot 'if'

走远了吗. 提交于 2019-12-02 14:13:10
Since C++ 17 one can write an if block that will get executed exactly once like this: #include <iostream> int main() { for (unsigned i = 0; i < 10; ++i) { if (static bool do_once = true; do_once) { // Enter only once std::cout << "hello one-shot" << std::endl; // Possibly much more code do_once = false; } } } I know I might be overthinking this, and there are other ways to solve this, but still - is it possible to write this somehow like this, so there is no need of the do_once = false at the end? if (DO_ONCE) { // Do stuff } I'm thinking a helper function, do_once() , containing the static

When does type information flow backwards in C++?

依然范特西╮ 提交于 2019-12-02 14:05:48
I just watched Stephan T. Lavavej talk at CppCon 2018 on "Class Template Argument Deduction", where at some point he incidentally says: In C++ type information almost never flows backwards ... I had to say "almost" because there's one or two cases, possibly more but very few . Despite trying to figure out which cases he might be referring to, I couldn't come up with anything. Hence the question: In which cases the C++17 standard mandates that type information propagate backwards? Here is at least one case: struct foo { template<class T> operator T() const { std::cout << sizeof(T) << "\n";

Member function template of class template can't find definition despite explicit instantiation present. Doesn't link

帅比萌擦擦* 提交于 2019-12-02 12:02:10
问题 Edit: This is not a duplicate of the linked question since I am using explicit instantiation and only a specific type of member functions do not link (others do). The following code compiles but doesn't link and I don't understand why. It is explicitly instantiating the Vector class to limit the number of possible arguments for T and therefore hides the definition of Vector<T> in a .cpp file. // fwd_decl.hpp #pragma once template<typename T> struct Vector; // Forward declare Vector to be used

Why can't I change the 'last write time' of my newly created files?

我怕爱的太早我们不能终老 提交于 2019-12-02 11:59:49
First off, I'm using Visual Studio 2015's implementation of the Filesystem library from the upcoming C++17 standard, which is based on Boost::Filesystem. Basically, what I'm trying to do is save a file's timestamp (it's "last write time"), copy that file's contents into an archive along with said timestamp, then extract that file back out and use the saved timestamp to restore the correct "last write time". // Get the file's 'last write time' and convert it into a usable integer. __int64 timestamp = chrono::time_point_cast<chrono::seconds>(fs::last_write_time(src)).time_since_epoch().count();

Why am I still getting undefined reference errors linking a static library with CMake in Android NDK?

跟風遠走 提交于 2019-12-02 11:56:05
When building the following Android-NDK project I am getting dozens of undefined reference errors regarding missing standard library functions. I followed some basic examples of linking static libraries and restarted this project from scratch 3 times but I still can't find the issue. I am trying to use the lib_seal library that I compiled using -std=c++1z . ( https://www.microsoft.com/en-us/research/project/simple-encrypted-arithmetic-library/ ). The errors I am getting indicate the library is linked properly but according to many references online it cannot find -lstdc++ (One example

Error C1202 (stack overflow) when recursively computing a templated value or function when using a conditional operator

我的未来我决定 提交于 2019-12-02 10:12:41
问题 I am implementing functionality that provides the opportunity to translate the coordinates of the cells of the game board to the number of this cell. This is what I'm trying (and failing) to make work. #include <cstdint> #include <utility> using UInt32 = std::uint32_t; template<UInt32... s> using IndexSequence = std::integer_sequence<UInt32, s...>; static constexpr UInt32 W = 8; static constexpr UInt32 H = 8; template<UInt32 x1, UInt32 x, UInt32 x2, UInt32 y1, UInt32 y2, UInt32... s> static