What do compilers do with compile-time branching?

TL;DR There are several ways to get different run-time behavior dependent on a template parameter. Performance is usually equal, so flexibility and maintainability are the main concern. In all cases, the various thin wrappers and constant conditional expressions will all be optimized away on any decent compiler for release builds. Below a small summary with … Read more

Check if a variable type is iterable?

You may create a trait for that: namespace detail { // To allow ADL with custom begin/end using std::begin; using std::end; template <typename T> auto is_iterable_impl(int) -> decltype ( begin(std::declval<T&>()) != end(std::declval<T&>()), // begin/end and operator != void(), // Handle evil operator , ++std::declval<decltype(begin(std::declval<T&>()))&>(), // operator ++ void(*begin(std::declval<T&>())), // operator* std::true_type{}); template <typename T> std::false_type … Read more

Which cannot be implemented without compiler hooks?

I have written up a complete answer here — it’s a work in progress, so I’m giving the authoritative hyperlink even though I’m cutting-and-pasting the text into this answer. Also see libc++’s documentation on Type traits intrinsic design. is_union is_union queries an attribute of the class that isn’t exposed through any other means; in C++, … Read more

How do I switch/select types during compile-time?

This should work: template<std::size_t N, typename… T> using static_switch = typename std::tuple_element<N, std::tuple<T…> >::type; Another method: template<std::size_t N, typename T, typename… Ts> struct static_switch { using type = typename static_switch<N – 1, Ts…>::type; }; template<typename T, typename… Ts> struct static_switch<0, T, Ts…> { using type = T; };

Find out whether a C++ object is callable

I think this trait does what you want. It detects operator() with any kind of signature even if it’s overloaded and also if it’s templatized: template<typename T> struct is_callable { private: typedef char(&yes)[1]; typedef char(&no)[2]; struct Fallback { void operator()(); }; struct Derived : T, Fallback { }; template<typename U, U> struct Check; template<typename> static … Read more

Difference between char and signed char in c++?

There are three distinct basic character types: char, signed char and unsigned char. Although there are three character types, there are only two representations: signed and unsigned. The (plain)char uses one of these representations. Which of the other two character representations is equivalent to char depends on the compiler. In an unsigned type, all the … Read more

Disambiguate overloaded member function pointer being passed as template parameter

The issue is here: l.call(&foo::func, “hello”); l.call(&foo::func, 0.5); For both lines, the compiler doesn’t know which foo::func you are referring to. Hence, you have to disambiguate yourself by providing the type information that is missing (i.e., the type of foo:func) through casts: l.call(static_cast<void (foo::*)(const std::string&)>(&foo::func), “hello”); l.call(static_cast<void (foo::*)(const double )>(&foo::func), 0.5); Alternatively, you can provide … Read more

What is the purpose of C++20 std::common_reference?

common_reference came out of my efforts to come up with a conceptualization of STL’s iterators that accommodates proxy iterators. In the STL, iterators have two associated types of particular interest: reference and value_type. The former is the return type of the iterator’s operator*, and the value_type is the (non-const, non-reference) type of the elements of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)