标签:
Whatever we do, we should preserve C++’s fundamental strengths:
• A direct map to hardware (initially from C)
• Zero-overhead abstraction (initially from Simula)
Depart from these and the language is no longer C++.
import std.vector;
import std.string;
import std.iostream;
import std.iterator;
//abc.cpp (文件名无需和module名重合)
module abc;
struct internal { };
int internal_f() { }
export namespace abc
{
class abc { };
}
int factorial(int n) {
int m;
Match(n) {
Case (1) return 0;
Case (m) return m*factorial(m-1);
Case (_) throw std::invalid_argument("");
} EndMatch
}
std::future<std::size_t> fut =
socket.async_read_some(buffer(buffer_space), use_future);
// ...
std::size_t length = fut.get();
socket.async_read_some(buffer(buffer_space), use_future).then([](std::size_t) {});
void coro_connection(tcp::socket& socket, yield_context yield)
{
try
{
std::vector<char> buffer_space(1024);
for (;;)
{
std::size_t length = socket.async_read_some(buffer(buffer_space), yield);
uppercase(buffer_space.begin(), buffer_space.begin() + length);
async_write(socket, buffer(buffer_space, length), yield);
}
}
catch (std::system_error& e)
{
// ...
}
}
tcp::iostream s("www.boost.org", "http");
s << "GET / HTTP/1.0\r\n";
s << "Host: www.boost.org\r\n";
s << "Accept: */*\r\n";
s << "Connection: close\r\n\r\n";
std::string header;
while (std::getline(s, header) && header != "\r")
std::cout << header << "\n";
std::cout << s.rdbuf();
generator<int> fib(int n) {
int a = 0;
int b = 1;
while (n-- > 0) {
yield a;
auto next = a + b;
a = b;
b = next;
}
}
goroutine pusher(channel<int>& left, channel<int>& right)
{
for(;;) {
auto val = await left.pull();
await right.push(val + 1);
}
}
int main() {
static const int N = 1000 * 1000;
std::vector<channel<int>> c(N + 1);
for(int i = 0; i < N; ++i)
goroutine::go(pusher(c[i], c[i + 1]));
c.front().sync_push(0);
std::cout << c.back().sync_pull() << std::endl;
}
sort(begin(v), end(v)); //无并行
sort(seq, begin(v), end(v)); //无并行
sort(pal, begin(v), end(v)); //并行
sort(par_vec, begin(v), end(v)); //并行矢量化
execution_policy exec=seq; //动态决定
if (v.size() > 1000) exec = par;
sort(exec, begin(v), end(v));
auto M = 32;
auto N = 64;
auto v = vector<float>(M * N);
auto av = array_view<float, 2>({M, N}, v);
For copy-list-initialization, auto deduction will either deduce a std::initializer_list (if the types of entries in the braced-init-list are all identical) or be ill-formed otherwise.
For direct list-initialization:
1.For a braced-init-list with only a single element, auto deduction will deduce from that entry;
2.For a braced-init-list with more than one element, auto deduction will be ill-formed.
unique_ptr<Foo const * const []> ptr1(new Foo*[10]);
Foo const * ptr = ptr1[9];
namespace A::B::C {
//…
}
namespace A {
namespace B {
namespace C {
//…
}
}
}
template<typename... T>
auto sum(T... s){
return (... + s);
}
T& operator[](size_t i) [[expects: i < size()]];
ArrayView(const vector<T>& v) [[ensures: data() == v.data()]];
void f(FILE* file)
{
fseek(file,9,SEEK_SET);
}
//proposed new C++ code
void f(FILE* file)
{
file->fseek(9,SEEK_SET); //nice autocomplete after "->"
}
template <class F, class Tuple, std::size_t... I>
constexpr decltype(auto) apply_impl(F&& f, Tuple&& t, std::index_sequence<I...>)
{
return std::invoke(std::forward<F>(f), std::get<I>(std::forward<Tuple>(t))...);
// Note: std::invoke is a C++17 feature
}
template <class F, class Tuple>
constexpr decltype(auto) apply(F&& f, Tuple&& t)
{
return apply_impl(std::forward<F>(f), std::forward<Tuple>(t),
std::make_index_sequence < std::tuple_size<std::decay_t<Tuple>>::value > {});
}
INVOKE的概念一直在标准里面,这回终于有了真正的invoke了。
template<typename Iterator,
typename = void>
struct reference_type
{
using type = decltype(*declval<Iterator>()); // no reference, use operator*
};
template<typename Iterator>
struct reference_type<Iterator,
void_t<typename Iterator::reference>
>
{
using type = typename Iterator::reference; //I have a reference
};
namespace std {
namespace experimental {
inline namespace fundamentals_v2 {
// See C++14 §20.10.4.1, primary type categories
template <class T> constexpr bool is_void_v
= is_void<T>::value;
template <class T> constexpr bool is_null_pointer_v
= is_null_pointer<T>::value;
//....
Ranges!这个我必须说一说。我们经常写
std::sort(std::begin(v),std::end(v),std::greater<>{});
那个begin end太烦了。Ranges就是为了解决这个问题:
std::sort(v,std::greater<>{});
当然远远不止这点,Ranges 里面的东西还可以花样组合。你还可以写出这样的东西:
int total = accumulate(view::iota(1) |
view::transform([](int x){return x*x;}) |
view::take(10), 0);
这个真的是深得我心啊!我最近正在按照目前的TS Draft实现这个东西。就是说一个vector:
std::vector<int,std::allocator<int>> v1;
std::vector<int,MyAllocator<int>> v2;
v1 = v2;//Error
由于Allocator属于类型的一部分,导致不同Allocator的vector不能copy啊等等。而且个人认为std::allocator有点鸡肋。这回好了,有了一个叫做memory_resource的抽象类:
class memory_resource {
// For exposition only
static constexpr size_t max_align = alignof(max_align_t);
public:
virtual ~memory_resource();
void* allocate(size_t bytes, size_t alignment = max_align);
void deallocate(void* p, size_t bytes,
size_t alignment = max_align);
bool is_equal(const memory_resource& other) const noexcept;
protected:
virtual void* do_allocate(size_t bytes, size_t alignment) = 0;
virtual void do_deallocate(void* p, size_t bytes,
size_t alignment) = 0;
virtual bool do_is_equal(const memory_resource& other) const noexcept = 0;
};
之后有五种内置的多态allocator:
有一个pmr::polymorphic_allocator的类满足Allocator requirements,将一个memory_resource包装起来:
#include <deque>
namespace std {
namespace experimental {
inline namespace fundamentals_v2 {
namespace pmr {
template <class T>
using deque = std::deque<T,polymorphic_allocator<T>>;
} // namespace pmr
} // namespace fundamentals_v2
} // namespace experimental
} // namespace std
#include <forward_list>
namespace std {
namespace experimental {
inline namespace fundamentals_v2 {
namespace pmr {
template <class T>
using forward_list =
std::forward_list<T,polymorphic_allocator<T>>;
} // namespace pmr
} // namespace fundamentals_v2
} // namespace experimental
} // namespace std
当然,我只是说了我了解的。还有很多其他的并发、并行算法、SIMD vector、string_view/array_view、optional/variant/any我没有做深入了解,就不误导大家了。
标签:
原文地址:http://www.cnblogs.com/destim/p/5544693.html