sfinae

SFINAE compiler troubles

一世执手 提交于 2019-12-04 09:34:03
问题 The following code of mine should detect whether T has begin and end methods: template <typename T> struct is_container { template <typename U, typename U::const_iterator (U::*)() const, typename U::const_iterator (U::*)() const> struct sfinae {}; template <typename U> static char test(sfinae<U, &U::begin, &U::end>*); template <typename U> static long test(...); enum { value = (1 == sizeof test<T>(0)) }; }; And here is some test code: #include <iostream> #include <vector> #include <list>

Using SFINAE with generic lambdas

谁说胖子不能爱 提交于 2019-12-04 07:56:08
Can generic lambdas take advantage of the "Substitution Failure Is Not An Error" rule ? Example auto gL = [](auto&& func, auto&& param1, auto&&... params) -> enable_if_t< is_integral< std::decay_t<decltype(param1)> >::value> { // ... }; auto gL = [](auto&& func, auto&& param1, auto&&... params) -> enable_if_t< !is_integral< std::decay_t<decltype(param1)> >::value> { // ... }; Are there any workarounds or plans to include this in the language ? Also since generic lambdas are templated function objects under the hood isn't it a bit odd that this can't be done ? Lambdas are function objects under

SFINAE - Trying to determine if template type has member function with 'variable' return type

点点圈 提交于 2019-12-04 07:32:07
Having trouble with SFINAE. I need to be able to determine if a Type has a member function operator-> defined regardless of its return type. Example follows. This class in the tester. It defines operator->() with a return type of X*. I thus will not know what 'X' is to hardcode it everywhere. template <class X> class PointerX { ... X* operator->() const; ... } This class tries to determines if the passed in T has a method operator-> defined; regardless of what operator-> return type is. template<typename T> struct HasOperatorMemberAccessor { template <typename R, typename C> static R

Choose template function based on existence of member

不问归期 提交于 2019-12-04 06:28:23
Let's say you have these two classes: class A { public: int a; int b; } class B { public: int a; int b; } class C { public: float a1; float b1; } enum class Side { A, B }; I want a template function which takes a side and a T , and depending on the T , returns a reference to " T.a " or " T.b " if the class has a member T::a , or a reference to " T.a1 " or " T.b1 " if the class has a member T::a1 . My starting point is: template<typename T> auto &GetBySide(const Side &side, const T &twoSided) { return side == Side::A?twoSided.a:twoSided.b; } template<typename T> auto &GetBySide(const Side &side

SFINAE Constructors [duplicate]

元气小坏坏 提交于 2019-12-04 03:48:23
问题 This question already has answers here : SFINAE working in return type but not as template parameter (3 answers) Template specialization and enable_if problems [duplicate] (1 answer) Closed last year . I have been liking SFINAE syntax like this for functions, seems to generally work well! template <class Integer, class = typename std::enable_if<std::is_integral<Integer>::value>::type> T(Integer n) { // ... } But am running into an issue when I want to do this as well in the same class...

Using SFINAE to detect a member function [duplicate]

混江龙づ霸主 提交于 2019-12-04 03:29:21
This question already has answers here : Closed 6 years ago . Is it possible to write a template to check for a function's existence? (25 answers) In C++11, to find out whether a class has a member function size , you could define the following test helper: template <typename T> struct has_size_fn { typedef char (& yes)[1]; typedef char (& no)[2]; template <typename C> static yes check(decltype(&C::size)); template <typename> static no check(...); static bool const value = sizeof(check<T>(0)) == sizeof(yes); }; Is there a similar trick for doing this in C++98 without relying on compiler

SFINAE and noexcept specifier

北战南征 提交于 2019-12-04 03:04:45
Does an expression in noexcept specifier's parentheses participate in SFINAE during overload resolution of function templates? I want to make an wrapper for aggregates and want the std::is_constructible predicate to work properly for it: template< typename type > struct embrace : type { template< typename ...arguments > embrace(arguments &&... _arguments) noexcept(noexcept(type{std::forward< arguments >(_arguments)...})) : type{std::forward< arguments >(_arguments)...} // braces { ; } }; int main() { struct S { int i; double j; }; // aggregate using E = embrace< S >; E b(1, 1.0); //

SFINAE and detecting if a C++ function object returns void

自闭症网瘾萝莉.ら 提交于 2019-12-04 01:07:31
问题 I've read the various authorities on this, include Dewhurst and yet haven't managed to get anywhere with this seemingly simple question. What I want to do is to call a C++ function object, (basically, anything you can call, a pure function or a class with ()), and return its value, if that is not void, or "true" otherwise. using std: struct Foo { void operator()() { cout << "Foo/"l; } }; struct Bar { bool operator()() { cout << "Bar/"; return true; } }; Foo foo; Bar bar; bool baz() { cout <<

mixing CRTP with SFINAE

喜你入骨 提交于 2019-12-03 21:46:17
I have a base taking derived type as template parameter. The following code works as expected. instantiation of base<non_default_impl> uses non_default_impl::data_t and base<default_impl> throws compilation error because event_data is only a forward declaration. template <typename T> struct event_data; template<typename T> struct tovoid { typedef void type; }; template <typename T, typename enable = void> struct get_data{ typedef event_data<T> type; }; template <typename T> struct get_data<T, typename tovoid<typename T::data_t>::type >{ typedef typename T::data_t type; }; template <typename T>

How does this has_member class template work?

不打扰是莪最后的温柔 提交于 2019-12-03 19:44:39
问题 I'm trying to understand how the following class template works (taken from here), but I couldn't understand it properly: template <typename Type> class has_member { class yes { char m;}; class no { yes m[2];}; struct BaseMixin { void operator()(){} }; struct Base : public Type, public BaseMixin {}; template <typename T, T t> class Helper{}; template <typename U> static no deduce(U*, Helper<void (BaseMixin::*)(), &U::operator()>* = 0); static yes deduce(...); public: static const bool result