CPP11 Tips - yszheda/wiki GitHub Wiki

variadic template

variadic function va_start, va_end, va_list, va_arg

CRTP (curiously recurring template pattern)

SFINAE

enum & template

enable_if

template<class T>
struct is_duration : std::false_type {};

template<class Rep, class Period>
struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type {};

check member in class

#include <type_traits>

template <typename T, typename = int>
struct HasX : std::false_type { };

template <typename T>
struct HasX <T, decltype((void) T::x, 0)> : std::true_type { };
template<typename T> struct HasX { 
    struct Fallback { int x; }; // introduce member name "x"
    struct Derived : T, Fallback { };

    template<typename C, C> struct ChT; 

    template<typename C> static char (&f(ChT<int Fallback::*, &C::x>*))[1]; 
    template<typename C> static char (&f(...))[2]; 

    static bool const value = sizeof(f<Derived>(0)) == 2;
}; 

struct A { int x; };
struct B { int X; };

int main() { 
    std::cout << HasX<A>::value << std::endl; // 1
    std::cout << HasX<B>::value << std::endl; // 0
}
// C++20
bool has_x(const auto &obj) {
    if constexpr (requires {obj.x;}) {
      return true;
    } else
      return false;
}
class foo
 { int x; };

struct bar
 { };

struct check_x_helper
 { int x; };

template <typename T>
struct check_x : public T, check_x_helper
 {
   template <typename U = check_x, typename = decltype(U::x)>
   static constexpr std::false_type check (int);

   static constexpr std::true_type check (long);

   using type = decltype(check(0));

   static constexpr auto value = type::value;
 };
namespace detail {
    struct P {typedef int member;};
    template <typename U>
    struct test_for_member : U, P
    {
        template <typename T=test_for_member, typename = typename T::member>
        static std::false_type test(int);
        static std::true_type test(float);
    };
}
template <typename T>
using test_for_member =
  std::integral_constant<bool, decltype(detail::test_for_member<T>::test(0)){}>;

.template

extern template

Copy elision

auto

decltype(auto)

int i;
auto x3a = i;                  // decltype(x3a) is int
decltype(auto) x3d = i;        // decltype(x3d) is int
auto x4a = (i);                // decltype(x4a) is int
decltype(auto) x4d = (i);      // decltype(x4d) is int&

lambda

recursive

cannot use auto

error: use of ‘get_factorial’ before deduction of ‘auto’

STL

std::integral_constant

std::tie

smart ptr

td::shared_ptr<int> sp( new int[10], std::default_delete<int[]>() );

function parameter

shared_ptr alias ctor

custom deleter

error: too many arguments for class template "std::shared_ptr"

error: static assertion failed with "constructed with null function pointer deleter"

type traits

std::decay

std::is_class

namespace detail {
template <class T>
std::integral_constant<bool, !std::is_union<T>::value> test(int T::*);
 
template <class>
std::false_type test(...);
}
 
template <class T>
struct is_class : decltype(detail::test<T>(nullptr))
{};
}
 
template <class T>
struct is_class : decltype(detail::test<T>(nullptr))
{};
namespace detail {
    template <class T> char test(int T::*);
    struct two { char c[2]; };
    template <class T> two test(...);
}
 
template <class T>
struct is_class : std::integral_constant<bool, sizeof(detail::test<T>(0))==1
                                            && !std::is_union<T>::value> {};

thread

constexpr

constexpr char constString[] = "constString";

constexpr auto constString = "constString";

// C++17
constexpr std::string_view sv = "hello, world";

ODR

attribute specifier sequence

attribute ignored in declaration deprecated

⚠️ **GitHub.com Fallback** ⚠️