sfinae

SFINAE: detect existence of a template function that requires explicit specialization

﹥>﹥吖頭↗ 提交于 2019-12-22 04:17:11
问题 As a follow-up to my previous question, I am trying to detect the existence of a template function that requires explicit specialization. My current working code detects non-template functions (thanks to DyP's help), provided they take at least one parameter so that dependent name lookup can be used: // switch to 0 to test the other case #define ENABLE_FOO_BAR 1 namespace foo { #if ENABLE_FOO_BAR int bar(int); #endif } namespace feature_test { namespace detail { using namespace foo; template

SFINAE enable_if explicit constructor

限于喜欢 提交于 2019-12-22 04:10:55
问题 I'm trying to switch between an explicit and an implicit conversion constructor via enable_if . My code currently looks like #include <type_traits> #include <cstdint> enum class enabled {}; template <bool B, typename T = void> using enable_if_t = typename std::enable_if<B, T>::type; template <bool B, typename T = void> using disable_if_t = typename std::enable_if<!B, T>::type; template <std::intmax_t A> struct SStruct { static constexpr std::intmax_t a = A; }; template <typename T> struct

How to extract the highest-indexed specialization from a structure?

给你一囗甜甜゛ 提交于 2019-12-22 03:46:53
问题 I'm trying to do some template metaprogramming and I'm finding the need to "extract" the highest index of a specialization of some structure in some type. For example, if I have some types: struct A { template<unsigned int> struct D; template<> struct D<0> { }; }; struct B { template<unsigned int> struct D; template<> struct D<0> { }; template<> struct D<1> { }; }; struct C { template<unsigned int> struct D; template<> struct D<0> { }; template<> struct D<1> { }; template<> struct D<2> { }; }

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

纵饮孤独 提交于 2019-12-21 20:15:13
问题 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

enable_if type is not of a certain template class

为君一笑 提交于 2019-12-21 12:23:57
问题 TLDR: See the last paragraph. I have an operator& defined for several template classes like so: template <typename T> struct Class { Class(T const &t) { } }; template <typename T_Lhs, typename T_Rhs> struct ClassAnd { ClassAnd(T_Lhs const &lhs, T_Rhs const &rhs) { } }; template <typename T, typename T_Rhs> ClassAnd<Class<T>, T_Rhs> operator&(Class<T> const &lhs, T_Rhs const &rhs) { return ClassAnd<Class<T>, T_Rhs>(lhs, rhs); } template <typename T0, typename T1, typename T_Rhs> ClassAnd

Using SFINAE to detect a member function [duplicate]

ⅰ亾dé卋堺 提交于 2019-12-21 09:15:48
问题 This question already has answers here : Is it possible to write a template to check for a function's existence? (25 answers) Closed 6 years ago . 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))

SFINAE: Compiler doesn't pick the specialized template class

回眸只為那壹抹淺笑 提交于 2019-12-21 09:13:15
问题 I have an SFINAE problem: In the following code, I want the C++ compiler to pick the specialized functor and print "special", but it's printing "general" instead. #include <iostream> #include <vector> template<class T, class V = void> struct Functor { void operator()() const { std::cerr << "general" << std::endl; } }; template<class T> struct Functor<T, typename T::Vec> { void operator()() const { std::cerr << "special" << std::endl; } }; struct Foo { typedef std::vector<int> Vec; }; int main

Why template instantiations go on forever here?

心不动则不痛 提交于 2019-12-21 06:39:22
问题 In the following code, I want to replace template <typename T, typename... Args> auto check (rank<1,T>, Args... args) const -> std::enable_if_t<!has_argument_type<T, Args...>(), decltype(check(rank<2, Ts...>{}, args...))> { return check(rank<2, Ts...>{}, args...); // Since rank<1,T> derives immediately from rank<2, Ts...>. } template <typename T, typename... Args> auto check (rank<2,T>, Args... args) const -> std::enable_if_t<!has_argument_type<T, Args...>(), decltype(check(rank<3, Ts...>{},

std::enable_if or SFINAE for iterator or pointer

折月煮酒 提交于 2019-12-21 04:55:11
问题 I would like to write a constructor for MyClass that take an argument and I want this to compile only if the argument is a pointer or an iterator (something having iterator_traits ). How to achieve this ? 回答1: Regrettably, there is no standard way to detect whether a class models Iterator . The simplest check would be that *it and ++it are both syntactically valid; you can do this using standard SFINAE techniques: template<typename T, typename = decltype(*std::declval<T&>(), void(), ++std:

SFINAE to assert() that code DOES NOT compile

六月ゝ 毕业季﹏ 提交于 2019-12-21 04:32:20
问题 I feel certain it must be possible to use SFINAE (possibly with Macros) to static_assert() that arbitary code will not compile. There are some complex cases in my code-base, where i have a class that I want to forbid taking temporaries (I believe the pattern is): class(const class&& tmp)=delete; /* deny copying from an unnamed temporary */ class (class&& rhs){/* allow construction from non-temporary rvalue*/} Currently, I check an undesired constructor DOES NOT compile, but then of course I