sfinae

Detect operator support with decltype/SFINAE

断了今生、忘了曾经 提交于 2019-11-27 19:08:54
A (somewhat) outdated article explores ways to use decltype along with SFINAE to detect if a type supports certain operators, such as == or < . Here's example code to detect if a class supports the < operator: template <class T> struct supports_less_than { static auto less_than_test(const T* t) -> decltype(*t < *t, char(0)) { } static std::array<char, 2> less_than_test(...) { } static const bool value = (sizeof(less_than_test((T*)0)) == 1); }; int main() { std::cout << std::boolalpha << supports_less_than<std::string>::value << endl; } This outputs true , since of course std::string supports

using SFINAE for template class specialisation

 ̄綄美尐妖づ 提交于 2019-11-27 18:59:25
suppose I have these declarations template<typename T> class User; template<typename T> class Data; and want to implement User<> for T = Data<some_type> and any class derived from Data<some_type> but also allow for other specialisations defined elsewhere. If I didn't already have the declaration of the class template User<> , I could simply template<typename T, typename A= typename std::enable_if<is_Data<T>::value>::type> class User { /*...*/ }; where template<template<typename> data>> struct is_Data { static const bool value = /* some magic here (not the question) */; }; However, this has two

How to call member function only if object happens to have it? [duplicate]

爱⌒轻易说出口 提交于 2019-11-27 15:37:04
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Is it possible to write a C++ template to check for a function's existence? I have a function f that receives a value val of type T (templated). Is there any way to call a member function on val only if the type has such a member function? Example: struct Bar { void foo() const {} }; template<class T> void f(T const& val) { // Is there any way to call *only* if foo() is available on type T? // SFINAE technique?

How to test whether class B is derived from template family of classes

限于喜欢 提交于 2019-11-27 15:17:06
How to test at compile time whether class B is derived from std::vector? template<class A> struct is_derived_from_vector { static const bool value = ????; }; How to test at compile time whether class B is derived from template family? template<class A, template< class > class Family> struct is_derived_from_template { static const bool value = ????; }; Using: template<class T> struct X {}; struct A : X<int> {} struct B : std::vector<char> {} struct D : X<D> {} int main() { std::cout << is_derived_from_template<A, X>::value << std::endl; // true std::cout << is_derived_from_template<D, X>::value

Is substitution performed on a variadic parameter pack type if the pack is empty?

主宰稳场 提交于 2019-11-27 15:09:57
Consider the following program: #include <type_traits> enum class dummy {}; template <typename T> using EnableIf = typename std::enable_if<T::value, dummy>::type; template <typename T> using DisableIf = typename std::enable_if<!T::value, dummy>::type; template <typename T> struct dependent_true_type : std::true_type {}; template <typename T, EnableIf<dependent_true_type<T>>...> std::true_type f(); template <typename T, DisableIf<dependent_true_type<T>>...> std::false_type f(); static_assert(decltype(f<int>())::value, ""); int main() {} GCC 4.7 glady accepts this program. My recent clang 3.1

Understanding SFINAE

一曲冷凌霜 提交于 2019-11-27 14:06:49
问题 As far as I know, SFINAE means substitution failures do not result in compilation errors, but just remove the prototype from the list of possible overloads. What I do not understand: why is this SFINAE: template <bool C, typename T = void> struct enable_if{}; template <typename T> struct enable_if<true, T> { typedef T type; }; But this is not? template <bool C> struct assert; template <> struct assert<true>{}; From my understanding, the underlying logic is identical here. This question

Select class constructor using enable_if

我们两清 提交于 2019-11-27 11:33:32
Consider following code: #include <iostream> #include <type_traits> template <typename T> struct A { int val = 0; template <class = typename std::enable_if<T::value>::type> A(int n) : val(n) {}; A(...) { } /* ... */ }; struct YES { constexpr static bool value = true; }; struct NO { constexpr static bool value = false; }; int main() { A<YES> y(10); A<NO> n; std::cout << "YES: " << y.val << std::endl << "NO: " << n.val << std::endl; } I want to selectively define constructor A::A(int) only for some types using enable_if. For all other types there is default constructor A::A(...) which should be

SFINAE works differently in cases of type and non-type template parameters

♀尐吖头ヾ 提交于 2019-11-27 09:12:59
Why does this code work: template< typename T, std::enable_if_t<std::is_same<T, int>::value, T>* = nullptr> void Add(T) {} template< typename T, std::enable_if_t<!std::is_same<T, int>::value, T>* = nullptr> void Add(T) {} and can correctly distinguish between these two calls: Add(1); Add(1.0); while the following code if compiled results in the redefinition of Add() error? template< typename T, typename = typename std::enable_if<std::is_same<T, int>::value, T>::type> void Add(T) {} template< typename T, typename = typename std::enable_if<!std::is_same<T, int>::value, T>::type> void Add(T) {}

Why doesn't SFINAE (enable_if) work for member functions of a class template?

南楼画角 提交于 2019-11-27 08:41:12
#include <type_traits> struct A{}; struct B{}; template <typename T> struct Foo { typename std::enable_if<std::is_same<T, A>::value>::type bar() {} typename std::enable_if<std::is_same<T, B>::value>::type bar() {} }; Error message: 14:5: error: 'typename std::enable_if<std::is_same<T, B>::value>::type Foo<T>::bar()' cannot be overloaded 10:5: error: with 'typename std::enable_if<std::is_same<T, A>::value>::type Foo<T>::bar()' Source on cpp.sh . I thought both typename std::enable_if<std::is_same<T,?>::value>::type could not be valid at the same time. Edit For posterity here is my edit based on

Detect if a default constructor exists at compile time [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 08:29:17
问题 This question already has an answer here: Is there a way to test whether a C++ class has a default constructor (other than compiler-provided type traits)? 7 answers I'm trying to check if a default constructor exists for a template argument. I want to do something like this: template <typename A> class Blah { Blah() { A* = new A(); } } But i want to detect at compile time via SFINAE or some other trick if that constructor exists, and raise a static_assert of my own if it doesn't. The problem