sfinae

Detecting whether something is (boost) range with SFINAE

*爱你&永不变心* 提交于 2019-12-23 18:43:54
问题 For logging code, I would like to detect whether given argument to a template function can be iterated over using the tools from Boost.Range or not. Obviously I need to instantiate different code whether it is or not, so I need SFINAE, possibly (well, certainly) combined with boost::enable_if. I've tried detecting whether begin and end free functions are defined, like this: namespace is_range_impl { template <typename T> T &make(); struct any { template <class T> any(T const&); }; struct not

Understanding a “template argument is invalid” error message

隐身守侯 提交于 2019-12-23 15:34:17
问题 Consider the code: #include <type_traits> #include <iostream> struct test1 { void Invoke() {}; }; struct test2 { template<typename> void Invoke() {}; }; enum class InvokableKind { NOT_INVOKABLE, INVOKABLE_FUNCTION, INVOKABLE_FUNCTION_TEMPLATE }; template<typename Functor, class Enable = void> struct get_invokable_kind { const static InvokableKind value = InvokableKind::NOT_INVOKABLE; }; template<typename Functor> struct get_invokable_kind< Functor, decltype(Functor().Invoke()) > { const

Overloading function when passing lambda as parameter

扶醉桌前 提交于 2019-12-23 14:56:02
问题 I'm trying to implement template function when return parameter is either void or T. I tried different variations of the code above using sfinae but still not sure if that is in general possible in case lamdba is function parameter. The following code does not compile : #include <functional> template <typename T> T Apply(const std::function<T()>& func) { return func(); } template <> void Apply(const std::function<void()>& func) { func(); } int main(int argc, char *argv[]) { int i1 = Apply([](

Overloading function when passing lambda as parameter

雨燕双飞 提交于 2019-12-23 14:53:05
问题 I'm trying to implement template function when return parameter is either void or T. I tried different variations of the code above using sfinae but still not sure if that is in general possible in case lamdba is function parameter. The following code does not compile : #include <functional> template <typename T> T Apply(const std::function<T()>& func) { return func(); } template <> void Apply(const std::function<void()>& func) { func(); } int main(int argc, char *argv[]) { int i1 = Apply([](

enable_if: minimal example for void member function with no arguments

喜欢而已 提交于 2019-12-23 09:30:10
问题 I am trying to get a better understanding of std::enable_if in C++11 and have been trying to write a minimal example: a class A with a member function void foo() that has different implementations based on the type T from the class template. The below code gives the desired result, but I am not understanding it fully yet. Why does version V2 work, but not V1 ? Why is the "redundant" type U required? #include <iostream> #include <type_traits> template <typename T> class A { public: A(T x) : a_

g++ and clang++ different behaviour with SFINAE and SFINAE failure

余生长醉 提交于 2019-12-23 09:28:07
问题 A couple of questions for C++11 experts. I'm fighting with SFINAE and I came across a strange case in which g++ (4.9.2), and clang++ (3.5.0) behave differently. I have prepared the following sample code. I'm sorry but I'm unable to do it significantly more concise. #include <string> #include <iostream> #include <typeinfo> #include <type_traits> template <typename X> class foo { private: template <typename R> using enableIfIsInt = typename std::enable_if<std::is_same<X, int>::value, R>::type;

Function Overloading Based on Arbitrary Properties of Types doesn't work

。_饼干妹妹 提交于 2019-12-22 12:39:32
问题 In the example below, I need to extract some values. I have an efficient extractor, which can work with builtin types, and an inefficient template that can work with everything. To choose between these, I want to use Function Overloading Based on Arbitrary Properties of Types. Here is my code: #include <string> #include <iostream> class extractor { public: static void extract(const bool& val) { std::cout << "Specialized extractor called" << std::endl; } static void extract(const double& val)

Template specialisation with default argument [duplicate]

血红的双手。 提交于 2019-12-22 09:29:43
问题 This question already has answers here : How does `void_t` work (2 answers) Closed last year . I have a program that is as follows. There is a base template struct X and a partial specialisation with SFINAE. template <typename T, typename U = void> struct X{ X() { std::cout << "in 1" << std::endl; }; }; template <typename T> struct X< T, std::enable_if_t<std::is_integral_v<T>> > { X() { std::cout << "in 2" << std::endl; }; }; int main() { X<int> x; } When running the program in 2 is printed.

enable_if seems to work outside a class but not inside

六眼飞鱼酱① 提交于 2019-12-22 08:55:10
问题 Here is my somewhat odd code: template <typename T&> class A { public: void b(typename std::enable_if<!std::is_pointer<T>::value, T>;::type o) {} void b(typename std::enable_if<std::is_pointer<T>::value, T>;::type o) {} }; template <typename T> void b(typename std::enable_if<!std::is_pointer<T>::value, T>::type o) {} template <typename T> void b(typename std::enable_if<std::is_pointer<T>::value, T>::type o) {} If I ifdef out the method b and call b<int *>(pi ) where pi is int * , everything

How can we modify the detection toolkit to check if a class has a member function with a specific signature?

隐身守侯 提交于 2019-12-22 06:44:52
问题 Given a (reduced) implementation of the detection idiom namespace type_traits { template<typename... Ts> using void_t = void; namespace detail { template<typename, template<typename...> class, typename...> struct is_detected : std::false_type {}; template<template<class...> class Operation, typename... Arguments> struct is_detected<void_t<Operation<Arguments...>>, Operation, Arguments...> : std::true_type {}; } template<template<class...> class Operation, typename... Arguments> using is