Skip to content

Instantly share code, notes, and snippets.

int a = 5;
decltype(a) b = a;
decltype(4+3+a) b = 4+3+a;
auto b = 4+3+a;
typedef std::pair<int,double> KeyObj;
bool my_cmp(const KeyObj& lhs, const KeyObj& rhs) {
return (lhs.first < rhs.first) ||
((lhs.first == rhs.first) && (lhs.second < rhs.second));
}
std::set<KeyObj, bool (*)(const KeyObj&, const KeyOnk&)> my_set(my_cmp);
my_set.insert( { 2, 3.4f } );
typedef std::pair<int,double> KeyObj;
struct my_cmp {
bool operator()(const KeyObj& lhs, const KeyObj& rhs) const {
return (lhs.first < rhs.first) ||
((lhs.first == rhs.first) && (lhs.second < rhs.second));
}
};
std::set<KeyObj, my_cmp> my_set;
my_set.insert( { 2, 3.4f } );
typedef std::pair<int,double> KeyObj;
auto my_cmp = [] (const KeyObj& lhs, const KeyObj& rhs) -> bool {
return (lhs.first < rhs.first) ||
((lhs.first == rhs.first) && (lhs.second < rhs.second));
};
std::set<KeyObj, decltype(my_cmp)> my_set(my_cmp);
my_set.insert( { 2, 3.4f } );
auto t1 = std::make_tuple( 10, 3.4f, "awesomeness" );
std::tuple<const int&, int> t2{ std::cref(std::get<0>(t1)), 20 };
auto t3 = std::tie( std::get<0>(t1), std::get<1>(t2) );
std::get<0>(t3) = 2;
assert( (std::get<0>(t1) == 2) && (std::get<0>(t2) == 2) && (std::get<0>(t3) == 2) );
// Define a type which holds an unsigned integer value
template<std::size_t> struct int_{};
template <class Tuple, size_t Pos>
std::ostream& print_tuple(std::ostream& out, const Tuple& t, int_<Pos> ) {
out << std::get< std::tuple_size<Tuple>::value-Pos >(t) << ',';
return print_tuple(out, t, int_<Pos-1>());
}
template <class Tuple>
std::cout << t1 << std::endl; // => (2,3.4f,"awesomeness")
std::cout << t2 << std::endl; // => (2,20)
std::cout << t3 << std::endl; // => (2,20)
auto f = std::unary_negate<int>() >> [](int c) -> int {return c*c;};
std::cout << f(4) << std::endl; // => -16