c++14

n={0,1,…,n-1} in C++

孤人 提交于 2020-01-06 06:55:42
问题 The formal definition (in set theory) of a natural number n is as follows: 0 is the empty set 1 = {0} n = {0,1,...,n-1} I think this would make some C++ code much simpler, if I was allowed to do this: for (int n : 10) cout << n << endl; and it printed numbers from 0 to 9. So I tried doing the following, which doesn't compile: #include <iostream> #include <boost/iterator/counting_iterator.hpp> boost::counting_iterator<int> begin(int t) { return boost::counting_iterator<int>(0); } boost:

How to stop template recursion while using parameter deduction?

核能气质少年 提交于 2020-01-06 04:37:08
问题 This code task a const char[] and finds where is the last slash: #include <array> #define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__) template< int PathIndex, int PathLength > constexpr const int findlastslash(const char (&path)[PathLength]) { constexpr const int end = PathLength - PathIndex; return (PathIndex >= 0 && path[end] != '/' && path[end] != '\\') ? findlastslash< PathIndex - 1, PathLength >( path ) : ( end + 1 ); } template< int PathLength > constexpr const int

optimise “binary_fold” algorithm and make it left (or right) associative

天大地大妈咪最大 提交于 2020-01-05 23:33:03
问题 Following my original question and considering some of the proposed solutions I came up with this for C++14: #include <algorithm> #include <exception> #include <iterator> #include <cstddef> template<class It, class Func> auto binary_fold(It begin, It end, Func op) -> decltype(op(*begin, *end)) { std::ptrdiff_t diff = end - begin; switch (diff) { case 0: throw std::out_of_range("binary fold on empty container"); case 1: return *begin; case 2: return op(*begin, *(begin + 1)); default: { //

optimise “binary_fold” algorithm and make it left (or right) associative

旧巷老猫 提交于 2020-01-05 23:32:10
问题 Following my original question and considering some of the proposed solutions I came up with this for C++14: #include <algorithm> #include <exception> #include <iterator> #include <cstddef> template<class It, class Func> auto binary_fold(It begin, It end, Func op) -> decltype(op(*begin, *end)) { std::ptrdiff_t diff = end - begin; switch (diff) { case 0: throw std::out_of_range("binary fold on empty container"); case 1: return *begin; case 2: return op(*begin, *(begin + 1)); default: { //

What compiler option/library do I need to use detect_or_t type trait?

我的梦境 提交于 2020-01-05 14:09:15
问题 I am trying to use std::experimental::detect_or_t from <experimental/type_traits> . What compiler, option, version or library do I need to compile the following example from http://en.cppreference.com/w/cpp/experimental/is_detected ? #include <experimental/type_traits> #include <cstddef> template<class T> using diff_t = typename T::difference_type; template <class Ptr> using difference_type = std::experimental::detected_or_t<std::ptrdiff_t, diff_t, Ptr>; struct Meow { using difference_type =

Cleanup at unexpected function end, std equivalent

我与影子孤独终老i 提交于 2020-01-05 08:23:06
问题 I have some function where I need to use a member variable(a vector of custom classes). At the end of this function this member needs to be cleared but it needs to stay as a member for the duration of this function. Another problem is that the function can end prematurely due to custom error handling of the program. Yet the member still needs to be cleared. I first moved this member at the beginning in a local variable using std::move. This worked pretty well but it now turns out I need the

Passing a generic function to operate on homogenous types

限于喜欢 提交于 2020-01-05 08:20:59
问题 Let's say I have a class composed of homogeneous types: struct Homog { int a; double b; std::string c; }; Here's a function which calculates the "element-wise min" 1 of two instances of this class: Homog min(const Homog& l, const Homog& r) { return { std::min(l.a, r.a), std::min(l.b, r.b), std::min(l.c, r.c), }; } Great. Now I want to calculate the max instead of the min . The code is identical, but with std::min replaced with std::max . I wouldn't want to duplicate it, rather I'd like to

Timing of lambda expression move capture

别等时光非礼了梦想. 提交于 2020-01-05 05:42:26
问题 When I use Boost.Asio, creating object such as ip::tcp::socket or deadline_timer as std::shared_ptr and copy captured it to the completion handler as lambda expression. I curious that what happens if I use move capture instead of copy capture. I think that it is dangerous. In the following example, I think that tim = std::move(tim) is evaluated before tim->async_wait . So tim no longer has valid pointer. It is my guess. In order to trace std::shared_ptr 's behavior, I created std::shared_ptr

Can I implicitly create a trivially copiable type

Deadly 提交于 2020-01-04 14:13:41
问题 My question is this: Suppose type T is trivially copyable....can "create" an instance of this type without calling a constructor....like so: #include <type_traits> #include <cstring> using T = int; // T can be any trivially copyable type T create(const T& other) { std::aligned_storage_t<sizeof(T),alignof(T)> my_T; std::memcpy(&my_T,&other,sizeof(T)); return *reinterpret_cast<T*>(&my_T); } Is this defined behavior, or can I only copy into an existing object of type T? 回答1: The rule, from

According to my interpretation of §3.4.1/8 this code should compile. What am I missing?

淺唱寂寞╮ 提交于 2020-01-04 13:58:15
问题 According to [basic.lookup.unqual]/8 from N4140 the following snippet should compile. But it doesn't in clang, gcc and vs2013. struct C { void f(I) {} using I = int; }; [basic.lookup.unqual]/8 (emphases are mine): For the members of a class X , a name used in a member function body, in a default argument, in an exception-specification , in the brace-or-equal-initializer of a non-static data member (9.2), or in the definition of a class member outside of the definition of X , following the