enable-if

SFINAE enable_if explicit constructor

限于喜欢 提交于 2019-12-22 04:10:55
问题 I'm trying to switch between an explicit and an implicit conversion constructor via enable_if . My code currently looks like #include <type_traits> #include <cstdint> enum class enabled {}; template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type; template <bool B, typename T = void> using disable_if_t = typename std::enable_if<!B, T>::type; template <std::intmax_t A> struct SStruct { static constexpr std::intmax_t a = A; }; template <typename T> struct

enable_if type is not of a certain template class

为君一笑 提交于 2019-12-21 12:23:57
问题 TLDR: See the last paragraph. I have an operator& defined for several template classes like so: template <typename T> struct Class { Class(T const &t) { } }; template <typename T_Lhs, typename T_Rhs> struct ClassAnd { ClassAnd(T_Lhs const &lhs, T_Rhs const &rhs) { } }; template <typename T, typename T_Rhs> ClassAnd<Class<T>, T_Rhs> operator&(Class<T> const &lhs, T_Rhs const &rhs) { return ClassAnd<Class<T>, T_Rhs>(lhs, rhs); } template <typename T0, typename T1, typename T_Rhs> ClassAnd

std::enable_if or SFINAE for iterator or pointer

折月煮酒 提交于 2019-12-21 04:55:11
问题 I would like to write a constructor for MyClass that take an argument and I want this to compile only if the argument is a pointer or an iterator (something having iterator_traits ). How to achieve this ? 回答1: Regrettably, there is no standard way to detect whether a class models Iterator . The simplest check would be that *it and ++it are both syntactically valid; you can do this using standard SFINAE techniques: template<typename T, typename = decltype(*std::declval<T&>(), void(), ++std:

enable class's member depending on template

会有一股神秘感。 提交于 2019-12-21 03:52:21
问题 I already know that you can enable (or not) a class's method using std::enable_if for exemple: template<size_t D, size_t E> class Field { ... size_t offset(const std::array<float,D>& p) const { ... } template<typename TT = size_t> typename std::enable_if<D!=E, TT>::type offset(const std::array<float,E>& p) const { return offset(_projection(p)); } ... }; This helps not being able to call function that are invalid in a specific case as well as removing overloading errors ... which, to me, is

How does std::enabled_if work when enabling via a parameter

家住魔仙堡 提交于 2019-12-20 04:23:42
问题 I'm trying to understand how enable_if works and I understand almost everything except scenario #3 from https://en.cppreference.com/w/cpp/types/enable_if template<class T> void destroy(T* t, typename std::enable_if<std::is_trivially_destructible<T>::value>::type* = 0) { std::cout << "destroying trivially destructible T\n"; } if the expression in enable_if is true then partial template specialization is chosen, so if it is chosen: why in enable_if is only condition without indicating second

SFINAE did not compile [duplicate]

蹲街弑〆低调 提交于 2019-12-20 02:53:01
问题 This question already has answers here : SFINAE working in return type but not as template parameter (3 answers) Closed 4 years ago . Very often I used SFINAE before but I have a very very simple example I can't get to run today. class X { public: template <typename CHECK, typename = typename std::enable_if< std::is_floating_point<CHECK>::value, void>::type > void Do() { std::cout << "yes" << std::endl; } template <typename CHECK, typename = typename std::enable_if< !std::is_floating_point

Use std::tuple for template parameter list instead of list of types

爷,独闯天下 提交于 2019-12-19 09:02:31
问题 I'm trying to make a call to a templated function like this : typedef std::tuple<int, double, bool> InstrumentTuple; Cache cache; InstrumentTuple tuple = cache.get<InstrumentTuple>(); I know I could "simply" pass the types of the tuple. This is what I do know but it is quite cumbersome since I make a lot of calls to this function and since the tuples are quite long: InstrumentTuple tuple = c.get<int, double, bool>(); // syntax I'd like to avoid So I tried multiple implementations of the get

What's the correct `enable_if` constraint on perfect forwarding setter?

那年仲夏 提交于 2019-12-18 11:46:47
问题 Herb Sutter's Back to the Basics! Essentials of Modern C++ presentation at CppCon discussed different options for passing parameters and compared their performance vs. ease of writing/teaching. The 'advanced' option (providing the best performance in all the cases tested, but too difficult for most developers to write) was perfect forwarding, with the example given (PDF, pg. 28): class employee { std::string name_; public: template <class String, class = std::enable_if_t<!std::is_same<std:

how can I use std::enable_if in a conversion operator?

巧了我就是萌 提交于 2019-12-17 20:12:28
问题 Basically I want my range type to be implicitly convertible from Range<const char> to Range<const unsigned char> . std::enable_if seems impossible because the function takes no arguments and has no return. Whats the work around? Here is basically what I tried: template<typename T> class Range{ T* begin_; T* end_; public: Range(T* begin,T* end):begin_{begin},end_{end}{} template<int N> Range(T (&a)[N]):begin_{static_cast<T*>(&a[0])},end_{static_cast<T*>(&a[N-1])}{} T* Begin(){return begin_;} T

Correct signature of / detect presence of Container::reserve()

拥有回忆 提交于 2019-12-17 18:55:14
问题 Given a type C which is an STL-conforming container, how do I correctly detect if C contains a member function reserve ? I tried the following approach (with GCC 4.6.3): template< typename C, typename = void > struct has_reserve : std::false_type {}; template< typename C > struct has_reserve< C, typename std::enable_if< std::is_same< decltype( &C::reserve ), void (C::*)( typename C::size_type ) >::value >::type > : std::true_type {}; This works for C being std::vector , but not for the