sfinae

SFINAE not happening with std::underlying_type

你离开我真会死。 提交于 2019-12-05 11:26:54
问题 Below SFINAE code with variadic templates compiles nicely using clang 3.7.1, C++14: #include <array> #include <iostream> #include <vector> #include <cstdint> enum class Bar : uint8_t { ay, bee, see }; struct S { static void foo() {} // std::begin(h) is defined for h of type H template<typename H, typename... T> static typename std::enable_if<std::is_pointer<decltype(std::begin(std::declval<H>()))*>::value>::type foo(const H&, T&&... t) { std::cout << "container\n"; foo(std::forward<T>(t)...);

enable_if type is not of a certain template class

为君一笑 提交于 2019-12-05 11:04:35
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<ClassAnd<T0, T1>, T_Rhs> operator&(ClassAnd<T0, T1> const &lhs, T_Rhs const &rhs) { return ClassAnd<ClassAnd

Problem with SFINAE

北城余情 提交于 2019-12-05 11:00:37
Why this code (fnc value in class M) do not get resolved by SFINAE rules? I'm getting an error: Error 1 error C2039: 'type' : is not a member of 'std::tr1::enable_if<_Test,_Type>' Of course type is not a member, it isn't defined in this general ver of enable_if but isn't the whole idea behind this to enable this ver of fnc if bool is true and do not instantiate it if it's false? Could please someone explain that to me? #include <iostream> #include <type_traits> using namespace std; template <class Ex> struct Null; template <class Ex> struct Throw; template <template <class> class Policy>

How to find if a method of a particular prototype exists inside a class?

心不动则不痛 提交于 2019-12-05 10:20:52
I'm working with some SFINAE features; currently in a portion of an application that must run in Linux and Windows; the compiler choices are MSVC (Visual Studio 2010 10.0) for Windows applications and GCC 4.4.5 for the Linux ones. I must check if some given object provides some functions to perform a custom serialization and call this functions, or do a simple memcpy and sizeof(Object) while the custom serialization methods are not provided. The problem is that a piece of code compile without warnings nor errors in MSVC but while compiling with GCC, the code is the following: template <

How to determine whether a class has a particular templated member function?

烂漫一生 提交于 2019-12-05 09:31:20
I was wondering if it's possible to extend the SFINAE approach to detecting whether a class has a certain member function (as discussed here: "Is there a Technique in C++ to know if a class has a member function of a given signature?" Check if a class has a member function of a given signature ) to support templated member functions? E.g. to be able to detect the function foo in the following class: struct some_class { template < int _n > void foo() { } }; I thought it might be possible to do this for a particular instantiation of foo, (e.g. check to see if void foo< 5 >() is a member) as

How to make static_assert play nice with SFINAE

会有一股神秘感。 提交于 2019-12-05 08:23:17
Update I posted a working rough draft of rebind as an answer to the question. Though I didn't have much luck finding a generic way to keep static_assert s from breaking metafunctions. Basically I want to check if a templated type T<U, Args...> can be constructed from some other type T<V, Args...> . Where T and Args... is the same in both types. The problem is, T<> might have a static_assert in it that totally breaks my metafunction. Below is a rough summary of what I'm trying to do. template<typename T> struct fake_alloc { using value_type = T; }; template<typename T, typename Alloc = fake

mixing CRTP with SFINAE

不羁岁月 提交于 2019-12-05 07:20:06
问题 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,

Why does the following code compile even though I have undefined member functions?

允我心安 提交于 2019-12-05 05:20:15
I was halfway through working on this piece of code and thought this is obviously not going to compile before hitting the build button. I was surprised that it not only compiled, but linked and worked as well. If I were to guess I would say that SFINAE is responsible for it compiling... is it? struct BaseClass { public: BaseClass() {} template<typename T> BaseClass(const T& a_other) { int i = 0; // for break point } template<typename T> BaseClass& operator= (const T& a_other) { int i = 0; // for break point return *this; } private: BaseClass(const BaseClass& a_other); // Does not have a

SFINAE: std::enable_if as function argument

蹲街弑〆低调 提交于 2019-12-05 05:09:41
So, I'm following the example set by the code somewhere on this web page: http://eli.thegreenplace.net/2014/sfinae-and-enable_if/ Here's what I have: template<typename T> void fun(const typename std::enable_if_t<std::is_integral<T>::value, T>& val) { std::cout << "fun<int>"; } template<typename T> void fun(const typename std::enable_if_t<std::is_floating_point<T>::value, T>& val) { std::cout << "fun<float>"; } int main() { fun(4); fun(4.4); } This way I would have to write: fun<int>(4); fun<double>(4.4); How would I avoid that? Compiler complains that it can't deduce the parameter T . Piotr

Use SFINAE to detect the existence of a templated member function

一世执手 提交于 2019-12-05 04:57:40
I learned SFINAE can be used to determine whether a member function exists in a class or not. For example, the following code can be used to check if the method hello is present in a class. struct has_method_hello { using yes = char[1]; using no = char[2]; template <typename U, typename C> static constexpr yes& test(decltype(&U::hello)); template <typename> static constexpr no& test(...); static constexpr bool value = (sizeof(yes) == sizeof(test<T>(nullptr))); }; struct Foo { void hello() {} } std::cout << has_method_hello <Foo> :: value << std::endl; // 1 However, suppose the hello is