sfinae

SFINAE with C++14 return type deduction

一曲冷凌霜 提交于 2019-11-28 11:21:49
Thanks to C++14, we'll soon be able to curtail verbose trailing return types; such as the generic min example from David Abrahams 2011 post : template <typename T, typename U> auto min(T x, U y) -> typename std::remove_reference< decltype(x < y ? x : y) >::type { return x < y ? x : y; } Under C++14 the return type can be omitted, and min can be written as: template <typename T, typename U> auto min(T x, U y) { return x < y ? x : y; } This is a simple example, however return type deduction is very useful for generic code, and can avoid much replication. My question is, for functions such as

std::hash specialization using sfinae?

此生再无相见时 提交于 2019-11-28 11:16:23
As an exercise I was trying to see if I could use SFINAE to create a std::hash specialization for std::pair and std::tuple when all of its template parameters are of an unsigned type. I have a little experience with them, but from what I understand the hash function needs to have already been templated with a typename Enabled = void for me to add a specialization. I'm not really sure where to go from here. Here's an attempt which doesn't work. #include <functional> #include <type_traits> #include <unordered_set> #include <utility> namespace std { template <typename T, typename Enabled = void>

Partial specialization of a method in a templated class

拈花ヽ惹草 提交于 2019-11-28 10:45:23
Given: struct A { virtual bool what() = 0; }; template<typename T, typename Q> struct B : public A { virtual bool what(); }; I want to partially specialize what like: template<typename T, typename Q> bool B<T, Q>::what() { return true; } template<typename Q> bool B<float, Q>::what() { return false; } But it appears that this isn't possible (is it in C++11?) so I tried SFINAE: template<typename T> typename std::enable_if<std::is_same<T, float>::value, bool>::type B<T>::what() { return true; } template<typename T> typename std::enable_if<!std::is_same<T, float>::value, bool>::type B<T>::what() {

C++11 style SFINAE and function visibility on template instantiation

拜拜、爱过 提交于 2019-11-28 09:52:57
问题 I'm not sure if this has anything to do with sfinae, or just something thats relevant for any templated function. I am attempting to use sfinae to enable/disable a member function based on existence of corresponding free function, which in turn is enabled/disabled based on existance of member function in another type, all using method described here: struct S; template <typename T> inline auto f(S& s, T const& t) -> decltype(t.f(s), void()) { t.f(s); } struct S { template <typename T> auto f

“Overload” function template based on function object operator() signature in C++98

若如初见. 提交于 2019-11-28 09:34:40
问题 I want to make a template function that takes a function and a vector and uses the function to map that vector to another vector that will be returned by the function template. If the function taken as an argument is a free function, it may have one of two signatures. // T is the parameter of the function template T sig1(const T x); T sig2(const T x, const std::vector<T>& v); It may also be a function object in which operator() would behave like the free functions. Use of the function

Assert that code does NOT compile

久未见 提交于 2019-11-28 09:09:26
In short: How to write a test, that checks that my class is not copyable or copy-assignable, but is only moveable and move-assignable? In general: How to write a test, that makes sure that a specific code does not compile? Like this: // Movable, but non-copyable class struct A { A(const A&) = delete; A(A&&) {} }; void DoCopy() { A a1; A a2 = a1; } void DoMove() { A a1; A a2 = std::move(a1); } void main() { // How to define these checks? if (COMPILES(DoMove)) std::cout << "Passed" << std::endl; if (DOES_NOT_COMPILE(DoCopy)) std::cout << "Passed" << std::endl; } I guess something to do with

SFINAE + sizeof = detect if expression compiles

廉价感情. 提交于 2019-11-28 07:00:23
I just found out how to check if operator<< is provided for a type. template<class T> T& lvalue_of_type(); template<class T> T rvalue_of_type(); template<class T> struct is_printable { template<class U> static char test(char(*)[sizeof( lvalue_of_type<std::ostream>() << rvalue_of_type<U>() )]); template<class U> static long test(...); enum { value = 1 == sizeof test<T>(0) }; typedef boost::integral_constant<bool, value> type; }; Is this trick well-known, or have I just won the metaprogramming Nobel prize? ;) EDIT: I made the code simpler to understand and easier to adapt with two global

Conditionally disabling a copy constructor

非 Y 不嫁゛ 提交于 2019-11-28 06:47:17
Suppose I'm writing a class template C<T> that holds a T value, so C<T> can be copyable only if T is copyable. Normally, when a template might or might not support a certain operation, you just define the operation, and it's up to your callers to avoid calling it when it's not safe: template <typename T> class C { private: T t; public: C(const C& rhs); C(C&& rhs); // other stuff }; However, this creates problems in the case of a copy constructor, because is_copy_constructible<C<T>> will be true even when T is not copyable; the trait can't see that the copy constructor will be ill-formed if it

If the address of a function can not be resolved during deduction, is it SFINAE or a compiler error?

萝らか妹 提交于 2019-11-28 05:24:16
In C++0x SFINAE rules have been simplified such that any invalid expression or type that occurs in the "immediate context" of deduction does not result in a compiler error but rather in deduction failure (SFINAE). My question is this: If I take the address of an overloaded function and it can not be resolved, is that failure in the immediate-context of deduction? (i.e is it a hard error or SFINAE if it can not be resolved)? Here is some sample code: struct X { // template<class T> T* foo(T,T); // lets not over-complicate things for now void foo(char); void foo(int); }; template<class U> struct

SFINAE: checking the existence of a function breaks when the overload is moved to other namespaces

爱⌒轻易说出口 提交于 2019-11-28 05:18:39
问题 I want to check for the existence of a function in a specific namespace using SFINAE. I have found SFINAE to test a free function from another namespace which does the job, but there are some things I don't understand. Currently I have this working code, straight from the linked question: // switch to 0 to test the other case #define ENABLE_FOO_BAR 1 namespace foo { #if ENABLE_FOO_BAR int bar(); #endif } namespace detail_overload { template<typename... Args> void bar(Args&&...); } namespace