sfinae

Is substitution failure an error with dependent non-type template parameters?

自闭症网瘾萝莉.ら 提交于 2019-12-04 18:46:44
问题 Let's say I have these template aliases: enum class enabler {}; template <typename T> using EnableIf = typename std::enable_if<T::value, enabler>::type; template <typename T> using DisableIf = typename std::enable_if<!T::value, enabler>::type; I can do the following in GCC: #include <iostream> template <typename T, EnableIf<std::is_polymorphic<T>> = {}> void f(T) { std::cout << "is polymorphic\n"; } template <typename T, DisableIf<std::is_polymorphic<T>> = {}> void f(T) { std::cout << "is not

Boost.Hana: How to check if function has specialisation for a certain type?

情到浓时终转凉″ 提交于 2019-12-04 18:10:31
I have a template function that has no definition by default but it specialised by some types: template <typename T> auto foo(bar &, const T &) -> void; template <> auto foo<std::string>(bar &, const std::string &) -> void {} How do I write a constexpr function that tells me if type T has a specialisation for the above function? My best effort: namespace detail { auto has_foo(hana::is_valid([](auto &b, const auto &t) -> decltype(foo(b, t)) {})); } // namespace detail template <typename T> constexpr auto has_foo() -> bool { using hana::type_c; return detail::has_foo(type_c<bar>, type_c<T>); }

Check if member function exists and is not inherited for SFINAE

。_饼干妹妹 提交于 2019-12-04 17:40:36
How can I check if a member function exists and is not inherited? I need this to resolve ambiguity for the following example: A type either has a foo() or a bar() member function. Caller will call the one that exists for the given type. However, DerivedWithBar inherits foo() from BaseWithFoo but defines its own bar() . Thus, Caller does not know which function to call. I'd need a way to give the non-inherited foo precedence over the inherited bar() , but I do not know how to check if a member function is inherited or not. #include <iostream> struct BaseWithFoo { template <typename T> void foo

Multiple SFINAE class template specialisations using void_t

笑着哭i 提交于 2019-12-04 15:39:13
问题 Are multiple class template specialisations valid, when each is distinct only between patterns involving template parameters in non-deduced contexts? A common example of std::void_t uses it to define a trait which reveals whether a type has a member typedef called "type". Here, a single specialisation is employed. This could be extended to identify say whether a type has either a member typedef called "type1", or one called "type2". The C++1z code below compiles with GCC, but not Clang. Is it

Using SFINAE to detect POD-ness of a type in C++

时光总嘲笑我的痴心妄想 提交于 2019-12-04 13:21:45
问题 The original title here was Workaround for SFINAE bug in VS2005 C++ This is tentative use of SFINAE to make the equivalent for the is_pod template class that exists in TR1 (In VS2005 there's no TR1 yet). It should have its value member true when the template parameter is a POD type (including primitive types and structs made of them) and false when it's not (like with non-trivial constructors). template <typename T> class is_pod { public: typedef char Yes; typedef struct {char a[2];} No;

how to SFINAE for enabling member function returning `auto`

放肆的年华 提交于 2019-12-04 13:10:26
In template meta programming, one can use SFINAE on the return type to choose a certain template member function, i.e. template<int N> struct A { int sum() const noexcept { return _sum<N-1>(); } private: int _data[N]; template<int I> typename std::enable_if< I,int>::type _sum() const noexcept { return _sum<I-1>() + _data[I]; } template<int I> typename std::enable_if<!I,int>::type _sum() const noexcept { return _data[I]; } }; However, this won't work if the function in question ( _sum() in above example) has auto-detected return type, such as _func() in this example template<int N> class A { /*

Can we use the detection idiom to check if a class has a member function with a specific signature?

蓝咒 提交于 2019-12-04 12:43:30
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_detected = detail::is_detected<void_t<>, Operation, Arguments...>; template<template<class...> class

Is it possible to specialize a template definition based on the existence of a nested typedef of a template type parameter?

风格不统一 提交于 2019-12-04 12:35:07
问题 I have a template, template <typename T> class wrapper , that I would like to specialize based on the existence of typename T::context_type . If typename T::context_type is declared, then the constructors and assignment operator overloads of the wrapper<T> instantiation should accept a mandatory typename T::context_type parameter. Additionally, wrapper<T> objects would store "context" in the member data. If typename T::context_type does not exist, then the constructors and assignment operator

Detect same class inheritance with SFINAE

大兔子大兔子 提交于 2019-12-04 10:55:36
I'm trying to write a metafunction that checks whether all types passed as a variadic template parameter are distinct. It seems that the most performant way to do this is to inherit from a set of classes and detect, whether there is an error. The problem is that compilation fails in the following code, while I would expect SFINAE to work. Edit. The question is not "how to write that metafunction" but "how do I catch that double inheritance error and output false_type when it happens". AFAIK, it's possible only with SFINAE. template <typename T> struct dummy {}; // error: duplicate base type

Do non-dependent default template arguments of function templates allow for SFINAE?

我与影子孤独终老i 提交于 2019-12-04 10:32:00
问题 With "non-dependent" here I mean "non-dependent on any other template arguments of that specific function template". While answering this question, I thought I found the answer, but according to @Johannes (in the comments to my answer), I'm misinterpreting the standard here. Take the following simple example: #include <type_traits> template<class T> struct X{ template<class U = typename T::type> static void foo(int){} static void foo(...){} }; int main(){ X<std::enable_if<false>>::foo(0); }