Category : sfinae

Before closing this as a duplicate (since there are several questions like this which did actually explain how to achieve the solution – this, and this), I did go through the similar questions, but after doing the same I still get hit by the ‘enable_if’ cannot be used to disable this declaration. So here is ..

Read more

While studying various std::vector‘s iterator implementations, I noticed this copy-constructor which uses SFINAE to allow initializing constant iterators from non-constant ones and vice versa: // Allow iterator to const_iterator conversion // … // N.B. _Container::pointer is not actually in container requirements, // but is present in std::vector and std::basic_string. template<typename _Iter> __normal_iterator(const __normal_iterator<_Iter, typename __enable_if< ..

Read more

I want to check if a given class has only the following: Non-static data members Constructor(s) (default or user-defined) Destructor (default or user-defined) This type would be (at least visually declaration-wise) identical to a POD-type apart from the user-defined constructor and destructor. I’ve tried to find a term for this kind of type but I ..

Read more

Looking to get some detailed explanations as to why the following is allowed / disallowed. #include <type_traits> #include <iostream> template<typename T> std::enable_if_t<std::is_arithmetic_v<T>> foo(T v) { std::cout << "arithmetic version calledn"; } template<typename T> std::enable_if_t<!std::is_arithmetic_v<T>> foo(T v) { std::cout << "non arithmetic version calledn"; } int main() { foo(33); foo("hello"); } In relation to the above ..

Read more

Apologies if any of the terminology is wrong here. What I’m trying to understand is – techniques like template metaprogramming and SFINAE/concepts in C++20 can both compute and remove branches at compile-time. What I can’t seem to find an answer for is, does that remain branchless at runtime and/or is the act of finding which ..

Read more