Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Created February 25, 2014 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derrickturk/9218535 to your computer and use it in GitHub Desktop.
Save derrickturk/9218535 to your computer and use it in GitHub Desktop.
#include <iostream>
constexpr int fac(int n)
{
return n <= 0 ? 1 : n * fac(n - 1);
}
struct [[weenie]] C {
void f() && { std::cout << "dying C's last gasp!\n"; }
void f() & { std::cout << "living, breathing C\n"; }
int mvar;
};
class D {
public:
D(int x) : x_(x) {}
private:
int x_;
};
class alignas(alignof(double)) E : public D {
public:
using D::D;
E() : E(47) {}
};
template<typename T> void g(const T& t)
{
std::cout << t.y << '\n';
}
using temperature = double;
inline constexpr long double operator"" _degC (long double val)
{
return val;
}
inline constexpr long double operator"" _degF (long double val)
{
return val * (9.0 / 5.0) + 32.0;
}
template<class... Numeric> struct sum_result;
template<class Numeric1, class... Numeric>
struct sum_result<Numeric1, Numeric...> {
typedef decltype(
Numeric1() +
typename sum_result<Numeric...>::result_type())
result_type;
};
template<class Numeric>
struct sum_result<Numeric> {
typedef Numeric result_type;
};
template<class Numeric>
auto sum(Numeric n) -> Numeric { return n; }
template<class Numeric1, class... Numeric>
auto sum(Numeric1 n, Numeric... rest)
-> typename sum_result<Numeric1, Numeric...>::result_type
{ return n + sum(rest...); }
int main()
{
int huh[fac(5)];
temperature cold = -23.0_degF, hot = 40.0_degC;
thread_local int x = 6;
std::cout << "sizeof(C::mvar) is " << sizeof(C::mvar) << '\n';
C().f();
C c;
c.f();
struct huzzah {
int y = 5;
} h;
g(h);
E e;
std::cout << "1 + 2.0 + 3u + 7.5f = " << sum(1, 2.0, 3u, 7.5f) << '\n';
#if __cplusplus > 201103L
auto f = [](auto x) { return x * 5; };
std::cout << "f(3) = " << f(3) << '\n';
std::cout << "f(3.2) = " << f(3.2) << '\n';
std::cout << "f('0') = " << f('0') << '\n';
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment