sfinae

Strange error with a templated operator overload

不打扰是莪最后的温柔 提交于 2019-11-29 13:38:44
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) { return Greater<L, Const<int> >(lhs, Const<int>(rhs)); } template<typename R> Greater<Const<int>, R>

Is it possible to use SFINAE/templates to check if an operator exists?

限于喜欢 提交于 2019-11-29 12:15:33
问题 I'm trying to check if an operator exists at compile time, if it doesn't I just want it ignored, is there any way to do that? example operator: template <typename T> QDataStream& operator<<(QDataStream& s, const QList<T>& l); 回答1: I ended up using a fallback namespace : namespace operators_fallback { template <typename T> inline QDataStream& operator<<(QDataStream& s, const T &) { return s; } template <typename T> inline QDataStream& operator>>(QDataStream& s, T &) { return s; } template

SFINAE: checking the existence of a function breaks when the overload is moved to other namespaces

拜拜、爱过 提交于 2019-11-29 11:14:26
I want to check for the existence of a function in a specific namespace using SFINAE. I have found SFINAE to test a free function from another namespace which does the job, but there are some things I don't understand. Currently I have this working code, straight from the linked question: // switch to 0 to test the other case #define ENABLE_FOO_BAR 1 namespace foo { #if ENABLE_FOO_BAR int bar(); #endif } namespace detail_overload { template<typename... Args> void bar(Args&&...); } namespace detail { using namespace detail_overload; using namespace foo; template<typename T> decltype(bar()) test

'if' with template parameters or SFINAE is preferred?

别来无恙 提交于 2019-11-29 10:59:02
问题 Preferred is this: template<typename T> bool isNotZero(const T &a) { if (std::is_floating_point<T>::value) return abs(a) > std::numeric_limits<T>::epsilon(); else return a; } Or this:? template<typename T> std::enable_if<std::is_floating_point<T>::value, bool>::type isNotZero(const T &a) { return abs(a) > std::numeric_limits<T>::epsilon(); } template<typename T> std::enable_if<std::is_integral<T>::value, bool>::type isNotZero(const T &a) { return a; } I usually use the first type to avoid

C++98/03 std::is_constructible implementation

时光毁灭记忆、已成空白 提交于 2019-11-29 10:20:52
The base components of my hobby library has to work with C++98 and C++11 compilers. To learn and to enjoy myself I created the C++98 implementations of several type support functionality (like enable_if , conditional , is_same , is_integral etc. ...) in order to use them when there is no C++11 support. However while I was implementing is_constructible I got stuck. Is there any kind of template magic (some kind of SFINAE) with which I can implement it without C++11 support ( declval )? Of course there is no variadic template support in C++03, so I will specialise the implementation till some

How to implement is_enum_class type trait? [duplicate]

帅比萌擦擦* 提交于 2019-11-29 09:51:44
This question already has an answer here: Is it possible to determine if a type is a scoped enumeration type? 1 answer 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. Based on your +T{} test: Option #1: Expression SFINAE in trailing return type: #include <type_traits> template <typename T> auto test(int) -> decltype((void)+T{}, std::false_type{}); template <typename T> auto test(...)

How to check if a template parameter is an iterator type or not?

耗尽温柔 提交于 2019-11-29 09:36:39
template<class T> struct is_iterator { static const bool value = ??? // What to write ??? }; int main() { assert(false == is_iterator<int>::value); assert(true == is_iterator<vector<int>::iterator>::value); assert(true == is_iterator<list<int>::iterator>::value); assert(true == is_iterator<string::iterator>::value); assert(true == is_iterator<char*>::value); // a raw pointer is also an iterator } The question is: How to make the five assert statements pass? Alexey Malistov template<class T> struct is_iterator { static T makeT(); typedef void * twoptrs[2]; // sizeof(twoptrs) > sizeof(void *)

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

為{幸葍}努か 提交于 2019-11-29 08:02:29
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? Following may help: ( https://ideone.com/nDlFUE ) #include <cstdint> #define DEFINE_HAS_SIGNATURE(traitsName, funcName, signature) \ template <typename U> \ class traitsName \ { \ private: \ template<typename T, T> struct helper; \ template<typename T> \ static std::uint8_t check(helper<signature,

How to simplify complicated SFINAE syntax, in pre-C++11, C++11, 14 and 17?

旧街凉风 提交于 2019-11-29 07:37:57
问题 This question was inspired by this answer. I wonder what are/were the best ways to simplify it in given standards. One I know and personally used/still use since C++14 is macro REQUIRES(x) : With definition: template<long N> struct requires_enum { enum class type { none, all }; }; #define REQUIRES(...) requires_enum<__LINE__>::type = \ requires_enum<__LINE__>::type::none, \ bool PrivateBool = true, \ typename std::enable_if<PrivateBool && (__VA_ARGS__), int>::type = 0 And use if even for non

How to write the best possible is_callable trait for templated operator()

偶尔善良 提交于 2019-11-29 06:14:26
I have is_callable trait defined like this: #ifndef IS_CALLABLE_HPP #define IS_CALLABLE_HPP #include <type_traits> namespace is_callable_detail { struct no {}; struct yes { no x[2]; }; template<bool CallableArgs, typename Callable, typename ReturnType, typename ...Args> struct check_return { static const bool value = std::is_convertible<decltype(std::declval<Callable>()(std::declval<Args>()...)), ReturnType>::value; }; template<typename Callable, typename ReturnType, typename ...Args> struct check_return<false, Callable, ReturnType, Args...> { static const bool value = false; }; } template