Created
July 20, 2018 13:22
-
-
Save ahamez/dea32d3a739727dde4c4d42e1c26b15f to your computer and use it in GitHub Desktop.
Some C++17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// g++-8 -std=c++17 -Wall -Wextra -fsanitize=address c++17.cc | |
#include <array> | |
#include <cstddef> | |
#include <iostream> | |
#include <set> | |
#include <sstream> | |
#include <string> | |
#include <string_view> | |
#include <tuple> | |
#include <variant> | |
namespace n1::n2 { | |
std::tuple<int, std::string> | |
foo(int i) | |
{ | |
return {i, std::to_string(i)}; | |
} | |
} | |
template <typename... Args> | |
int | |
bar(Args&&... args) | |
{ | |
return (1 + ... + args); | |
} | |
template <typename... Args> | |
void | |
baz(std::set<int>& s, Args&&... args) | |
{ | |
s.insert({args...}); | |
} | |
struct foo | |
{ | |
int x; | |
foo(int a) : x{a} {std::cout << "foo()\n";} | |
foo(const foo& rhs) : x{rhs.x} {std::cout << "foo(const foo&)\n";} | |
foo& operator=(const foo&){std::cout << "foo& operator=(const foo&)\n"; return *this;} | |
auto | |
fn0() | |
-> decltype(auto) | |
{ | |
return [this]{std::cout << x << '\n';}; | |
} | |
auto | |
fn1() | |
-> decltype(auto) | |
{ | |
return [*this]{std::cout << x << '\n';}; | |
} | |
}; | |
struct visitor | |
{ | |
void operator()(const foo&) {std::cout << "foo\n";} | |
void operator()(int) {std::cout << "int\n";} | |
void operator()(const std::string&){std::cout << "std::string\n";} | |
}; | |
int main() | |
{ | |
{ | |
auto s = std::set<int>{}; | |
const auto [cit, inserted] = s.insert(42); | |
std::cout << std::boolalpha << inserted << "\n"; | |
if (const auto [cit, inserted] = s.insert(33); inserted) | |
{ | |
std::cout << "inserted\n"; | |
} | |
else | |
{ | |
std::cout << "not inserted\n"; | |
} | |
if (const auto [cit, inserted] = s.insert(42); inserted) | |
{ | |
std::cout << "inserted\n"; | |
} | |
else | |
{ | |
std::cout << "not inserted\n"; | |
} | |
baz(s, 1, 2, 3); | |
} | |
{ | |
const auto [i, str] = n1::n2::foo(42); | |
std::cout << i << "," << str << '\n'; | |
if (const auto [i, str] = n1::n2::foo(33); i == 42) | |
{ | |
std::cout << "yaye!\n"; | |
} | |
else | |
{ | |
std::cout << "oooh…" << str << '\n'; | |
} | |
} | |
{ | |
std::cout << bar(1,2,3) << '\n'; | |
} | |
{ | |
const auto s = std::set<int>{1,2,3}; | |
auto ss = std::stringstream{}; | |
for (const auto x : s) | |
{ | |
ss << x << ","; | |
} | |
const auto s0 = ss.str(); | |
const auto view0 = std::string_view{s0}; | |
std::cout << view0 << '\n'; | |
const auto view1 = std::string_view{&s0[2], 3}; | |
std::cout << view1 << '\n'; | |
} | |
{ | |
const auto s0 = std::string{"foooo"}; | |
auto v0 = std::string_view(&*s0.cbegin(), 3); | |
v0.remove_prefix(1); | |
v0.remove_suffix(1); | |
std::cout << v0 << '\n'; | |
} | |
{ | |
[[maybe_unused]] const auto fn0 =[] | |
{ | |
auto z = foo{43}; | |
auto fn0 = z.fn0(); | |
z.x += 1; | |
return fn0; | |
// auto z = std::make_shared<foo>(43); | |
// auto fn0 = z->fn0(); | |
// z->x += 1; | |
// return fn0; | |
}(); | |
fn0(); // error, z is out of scope. | |
const auto fn1 =[] | |
{ | |
auto z = foo{43}; | |
auto fn1 = z.fn1(); | |
z.x += 1; | |
return fn1; | |
}(); | |
fn1(); | |
} | |
{ | |
const auto a = std::array<std::byte, 2>{{std::byte{0}, std::byte{255}}}; | |
for (const auto b : a) | |
{ | |
std::cout << std::to_integer<int>(b) << '\n'; | |
} | |
} | |
{ | |
using variant = std::variant<foo, int, std::string>; | |
const auto v0 = variant{foo{42}}; | |
std::visit(visitor{}, v0); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment