CPP11 Tips - yszheda/wiki GitHub Wiki
-
https://xenakios.wordpress.com/2014/01/16/c11-the-three-dots-that-is-variadic-templates-part/
-
http://stackoverflow.com/questions/1657883/variable-number-of-arguments-in-c
-
http://stackoverflow.com/questions/39792417/what-does-this-three-dots-means-in-c
-
[!]https://monoinfinito.wordpress.com/category/programming/c/c0x/
- http://stackoverflow.com/questions/16337459/undefined-number-of-arguments-in-c#16337614
- http://stackoverflow.com/questions/20901811/function-with-three-dots-argument
- https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern
- What is the curiously recurring template pattern (CRTP)?
- Curiously Recurring Template Pattern (CRTP), AutoLists and C++
- https://stackoverflow.com/questions/12726954/how-can-i-partially-specialize-a-class-template-for-all-enums
- https://stackoverflow.com/questions/1619993/template-specialization-for-enum
- Template SFINAE & type-traits
- https://stackoverflow.com/questions/1528374/how-can-i-extend-a-lexical-cast-to-support-enumerated-types/1528436#1528436
- https://stackoverflow.com/questions/4238710/specializing-template-for-enum
- https://stackoverflow.com/questions/4543720/is-it-possible-to-specialize-a-template-using-a-member-enum
- https://stackoverflow.com/questions/22721201/specializations-only-for-c-template-function-with-enum-non-type-template-param
-
http://www.enseignement.polytechnique.fr/informatique/INF478/docs/Cpp/en/cpp/types/enable_if.html
-
(Partially) specializing a non-type template parameter of dependent type
-
Check if a template parameter is some type of chrono::duration
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 {};
-
boost::enable_if_c error: is not a valid type for a template non-type parameter
-
How to use std::enable_if with a condition which itself depends on another condition?
-
Best way to handle multiple conditions for conditional compilation
-
https://en.wikibooks.org/wiki/More_C++_Idioms/Member_Detector
-
How to detect whether there is a specific member variable 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)){}>;
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&
cannot use auto
error: use of ‘get_factorial’ before deduction of ‘auto’
- https://stackoverflow.com/questions/14531993/can-lambda-functions-be-recursive
- https://stackoverflow.com/questions/2067988/recursive-lambda-functions-in-c11
-
https://stackoverflow.com/questions/11277249/how-to-pass-stdunique-ptr-around
-
https://stackoverflow.com/questions/9699333/passing-unique-ptr-to-functions
-
https://stackoverflow.com/questions/31135178/c-array-of-char-using-unique-ptr
-
https://stackoverflow.com/questions/37875781/unique-ptr-to-char-conversion
td::shared_ptr<int> sp( new int[10], std::default_delete<int[]>() );
- What is shared_ptr's aliasing constructor for?
- http://www.cplusplus.com/reference/memory/shared_ptr/shared_ptr/
- https://www.codesynthesis.com/~boris/blog/2012/04/25/shared-ptr-aliasing-constructor/
- How do I use a custom deleter with a std::unique_ptr member?
- Using custom deleter with std::shared_ptr
- Shared pointer with deleter
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> {};
constexpr char constString[] = "constString";
constexpr auto constString = "constString";
// C++17
constexpr std::string_view sv = "hello, world";
-
The One Definition Rule in C++11 and C++14: Constant Expressions and Inline Functions
-
https://isocpp.org/blog/2018/05/quick-q-use-of-constexpr-in-header-file
-
static constexpr class members are required to have a definition #788
-
Linker error for constexpr static member variable in gcc and clang
-
C++ - Breaking The Rules With Inline Variables and Functions