sfinae

function implementation with enable_if outside of class definition

[亡魂溺海] 提交于 2019-11-28 03:54:00
问题 So basically, I have a very basic generic class for now, currently testing the type_traits header. I am currently trying to make a function to work with certain types i.e arithmetic ones for now. #include <type_traits> template <typename T> class Test { public: template <typename U = T> typename std::enable_if<std::is_arithmetic<U>::value>::type print(); }; The function works perfectly and for arithmetic types only. But I like to keep my classes tidy and only have them have prototypes, while

conditional (SFINAE) override

坚强是说给别人听的谎言 提交于 2019-11-28 03:22:48
问题 I'm trying to do this: struct A { virtual int f() const { return 0; } }; template <typename T> struct B : A { template <typename U = T, typename std::enable_if<...some condition involving U...>::type> int f() const { return 1; } }; Caveat, I can't inherit class templates (use static overrides). Is this sort of construct allowed and can the template member B::f() override the member A::f()? 回答1: Try this: template <typename T, typename=void> struct B : A { ... }; temlate <typename T> struct B

How to implement is_enum_class type trait? [duplicate]

青春壹個敷衍的年華 提交于 2019-11-28 03:14:04
问题 This question already has an answer here : Is it possible to determine if a type is a scoped enumeration type? (1 answer) Closed last year . How can one implement type trait whose value member is true if and only if the passed in type T is a class enum? While I know that for instance +T{}; will work if T is an enum and fail if it is an enum class, I couldn't find a way so far to use this for SFINAE. 回答1: Based on your +T{} test: Option #1: Expression SFINAE in trailing return type: #include

boost::enable_if class template method

笑着哭i 提交于 2019-11-28 03:14:02
问题 I got class with template methods that looks at this: struct undefined {}; template<typename T> struct is_undefined : mpl::false_ {}; template<> struct is_undefined<undefined> : mpl::true_ {}; template<class C> struct foo { template<class F, class V> typename boost::disable_if<is_undefined<C> >::type apply(const F &f, const V &variables) { } template<class F, class V> typename boost::enable_if<is_undefined<C> >::type apply(const F &f, const V &variables) { } }; apparently, both templates are

Check if type is hashable

◇◆丶佛笑我妖孽 提交于 2019-11-28 02:35:30
问题 I would like to make a type trait for checking if a particular type is hashable using the default instantiations of the standard library's unordered containers, thus if it has a valid specialization for std::hash . I think this would be a very useful feature (e.g. for using std::set as failsafe for std::unordered_set in generic code). So I, thinking std::hash is not defined for each type, started making the following SFINAE solution: template<typename T> std::true_type hashable_helper( const

How to detect the presence of a static member function with certain signature?

牧云@^-^@ 提交于 2019-11-28 01:54:00
问题 I found several questions & answers on SO dealing with detecting at compile time (via SFINAE) whether a given class has a member of certain name, type, or signature. However, I couldn't find one that also applies to static public member functions (when pointer-to-member tricks won't work). Any ideas? 回答1: Following may help: (https://ideone.com/nDlFUE) #include <cstdint> #define DEFINE_HAS_SIGNATURE(traitsName, funcName, signature) \ template <typename U> \ class traitsName \ { \ private: \

SFINAE tried with bool gives compiler error: “template argument ‘T::value’ involves template parameter” [duplicate]

时间秒杀一切 提交于 2019-11-27 23:42:57
This question already has an answer here: Why is it disallowed for partial specialization in a non-type argument to use nested template parameters 2 answers I tried to implement an SFINAE using bool (unlike popular void_ trick ): template<typename T, bool = true> struct Resolve { static const bool value = false; }; template<typename T> struct Resolve<T, T::my_value> { static const bool value = true; }; The goal is to specialize, the classes which have static const bool my_value = true; defined inside it. If they are defined false or not defined then don't specialize it. i.e. struct B1 { //

How to decide if a template specialization exist

*爱你&永不变心* 提交于 2019-11-27 22:56:25
I would like to check if a certain template specialization exist or not, where the general case is not defined. Given: template <typename T> struct A; // general definition not defined template <> struct A<int> {}; // specialization defined for int I would like to define a struct like this: template <typename T> struct IsDefined { static const bool value = ???; // true if A<T> exist, false if it does not }; Is there a way to do that (ideally without C++11)? Thanks Using the fact that you can't apply sizeof to an incomplete type: template <class T, std::size_t = sizeof(T)> std::true_type is

Why do my SFINAE expressions no longer work with GCC 8.2?

我的未来我决定 提交于 2019-11-27 21:16:10
I recently upgraded GCC to 8.2, and most of my SFINAE expressions have stopped working. The following is somewhat simplified, but demonstrates the problem: #include <iostream> #include <type_traits> class Class { public: template < typename U, typename std::enable_if< std::is_const<typename std::remove_reference<U>::type>::value, int >::type... > void test() { std::cout << "Constant" << std::endl; } template < typename U, typename std::enable_if< !std::is_const<typename std::remove_reference<U>::type>::value, int >::type... > void test() { std::cout << "Mutable" << std::endl; } }; int main() {

Why does enable_if_t in template arguments complains about redefinitions?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 21:06:30
I have the following case that works using std::enable_if : template<typename T, typename std::enable_if<std::is_same<int, T>::value>::type* = nullptr> void f() { } template<typename T, typename std::enable_if<std::is_same<double, T>::value>::type* = nullptr> void f() { } Now, I saw in cppreference the new syntax, much cleaner in my opinion : typename = std::enable_if_t<std::is_same<int, T>::value>> I wanted to port my code : template<typename T, typename = std::enable_if_t<std::is_same<int, T>::value>> void g() { } template<typename T, typename = std::enable_if_t<std::is_same<double, T>: