sfinae

Check if class has function with signature

别来无恙 提交于 2019-12-18 09:22:16
问题 There are other answers on this site using SFINAE but with non C++11 code, and there are others using C++11 code like decltypes to make this process easier. However, I am not sure how to check if a class has a function with a specific signature. I want to check if a class has the function receive(const Event &) where Event is a class type that is specified when calling the check function. 回答1: The best way I know of is checking if you can actually call the function and if it returns the type

Check if two types are of the same template

梦想的初衷 提交于 2019-12-18 09:14:38
问题 I want to check if two types are of the same template. As an example I want the following snippet of code to return true because both objects are vectors despite the inner elements being of different types. It's important that the check is made at compile time (that's why the function is constexpr). #include <iostream> #include <type_traits> #include <vector> template <typename Container1, typename Container2> constexpr bool CheckTypes(Container1 c1, Container2 c2) { return std::is_same

Strange error with a templated operator overload

余生颓废 提交于 2019-12-18 07:39:58
问题 When I compile the following snippet, I get a compiler error with clang, but not with g++/MSVC: #include <string> template<typename T> struct Const { explicit Const(T val) : value(val) {} T value; }; template<typename T> struct Var { explicit Var(const std::string &n) : name(n) {} std::string name; }; template<typename L, typename R> struct Greater { Greater(L lhs, R rhs) : left(lhs), right(rhs) {} L left; R right; }; template<typename L> Greater<L, Const<int> > operator > (L lhs, int rhs) {

is_container trait fails on std::set SFINAE issue

杀马特。学长 韩版系。学妹 提交于 2019-12-18 07:08:13
问题 I am trying to write a stream operator for std containers, mainly for the purpose of debugging. I have the following code: #include <type_traits> #include <iostream> #include <ostream> #include <iterator> #include <algorithm> #include <functional> #include <vector> #include <set> #include <deque> template<typename Container> struct is_container { typedef char no; typedef long yes; template<typename A, A, A> struct is_of_type; template<typename T> static yes& is_cont( is_of_type < typename T:

C++ detecting free function existence with explicit parameters

谁说我不能喝 提交于 2019-12-18 04:11:08
问题 I'm writing some type traits to see if a free function exists with a specific set of parameters. The functions have a signature that looks something like this: template <class T> void func( SomeClass &, SomeType const & ); I know ahead of time the values for T , SomeClass , and SomeType . I want the trait to return true if this function exists with exactly these parameters, not using any implicit conversion. I can easily write some code to detect whether this function exists by using SFINAE

template overloading and SFINAE working only with functions but not classes

混江龙づ霸主 提交于 2019-12-18 02:16:46
问题 can someone explain why the compiler accepts only this code template<typename L, size_t offset, typename enable_if< (offset<sizeof(L)), int >::type =0> void a_function(){} template<typename L, size_t offset, typename enable_if< (offset==sizeof(L)), int >::type =0> void a_function(){} but not this: template<typename L, size_t offset, typename enable_if< (offset<sizeof(L)), int >::type =0> class a_class{}; template<typename L, size_t offset, typename enable_if< (offset==sizeof(L)), int >::type

Possible to use type_traits / SFINAE to find if a class defines a member TYPE?

流过昼夜 提交于 2019-12-18 00:42:10
问题 I have seen this question which allows one to check for the existence of a member function , but I'm trying to find out whether a class has a member type . In the example below, both evaluate to "false", but I would like to find a way so that has_bar<foo1>::value evaluates to false, and has_bar<foo2>::value evaluates to true . Is this possible? #include <iostream> struct foo1; struct foo2 { typedef int bar; }; template <typename T> class has_bar { typedef char yes; typedef long no; template

detecting typedef at compile time (template metaprogramming)

房东的猫 提交于 2019-12-17 23:32:46
问题 I am currently doing some template metaprogramming. In my case I can handle any "iteratable" type, i.e. any type for which a typedef foo const_iterator exists in the same manner. I was trying to use the new C++11 template metaprogramming for this, however I could not find a method to detect if a certain type is missing. Because I also need to turn on/off other template specializations based on other characteristics, I am currently using a template with two parameters, and the second one gets

SFINAE To detect non-member function existence

拟墨画扇 提交于 2019-12-17 20:55:53
问题 Does anybody know of a method for specializing a template depending on whether a non-member method is defined? I know there are numerous ways for specializing if a member function exists, but I've never seen a non-member example. The specific problem is specializing the operator<< for shared_ptr to apply the operator<< if the operator<< is defined for T, and printing the mere pointer location otherwise. It would be great if all classes defined operator<< as a member, but unfortunately many

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