sfinae

Why does order of declaring function changes overload chosen by SFINAE?

╄→尐↘猪︶ㄣ 提交于 2019-12-07 15:48:30
问题 This question is related to this answer. In this example SFINAE uses variable template has_literal_x specialization instead of the base template: struct A { }; A operator"" _x(char const*) { return {}; } template<typename T, typename S, typename = void> constexpr bool has_literal_x = false; template<typename T, typename S> constexpr bool has_literal_x<T, S, std::enable_if_t< std::is_same< decltype(operator""_x(std::declval<S>())), T >::value > > = true; int main() { std::cout << has_literal_x

What is the recommended way to simulate concepts and constraints? [closed]

感情迁移 提交于 2019-12-07 06:35:17
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 12 months ago . Before the introduction of concepts and constraints, there are several ways to simulate this compile-time check. Take a " order() " function for example: (how to implement LessThanComparable without concepts or constraints is another story) Use static_assert template

why SFINAE (enable_if) works from inside class definition but not from outside

非 Y 不嫁゛ 提交于 2019-12-07 06:27:09
问题 Very weird problem I've been struggling with for the past few hours (after solving 5-6 other issues with SFINAE as I'm new to it). Basically in the following code I want to have f() working for all possible template instantiations, but have g() available only when N == 2 : #include <type_traits> #include <iostream> template<typename T, int N> class A { public: void f(void); void g(void); }; template<typename T, int N> inline void A<T, N>::f() { std::cout << "f()\n"; } template<typename T, int

Problem with SFINAE

喜夏-厌秋 提交于 2019-12-07 05:42:13
问题 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

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

▼魔方 西西 提交于 2019-12-07 05:36:06
问题 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

How to make static_assert play nice with SFINAE

偶尔善良 提交于 2019-12-07 05:00:33
问题 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

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

ぐ巨炮叔叔 提交于 2019-12-07 04:48:39
问题 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

SFINAE: std::enable_if as function argument

丶灬走出姿态 提交于 2019-12-07 00:04:13
问题 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

Dependent non-type parameter packs: what does the standard say?

可紊 提交于 2019-12-06 22:58:49
问题 I think the following code is well-formed: template< typename T > using IsSigned = std::enable_if_t< std::is_signed_v< T > >; template< typename T, IsSigned< T >... > T myAbs( T val ); Others say that it is ill-formed, because §17.7 (8.3) of the C++17 standard: Knowing which names are type names allows the syntax of every template to be checked. The program is ill-formed, no diagnostic required, if: (...) every valid specialization of a variadic template requires an empty template parameter

Is there any guarantee on the order of substitution in a function template after type deduction?

喜你入骨 提交于 2019-12-06 20:46:09
问题 Consider this function template: template<typename T> typename soft_error<T>::type foo(T, typename hard_error<T>::type) { } After deducing type T from the type of the first argument in the call to foo() , the compiler will proceed to substitute T and instantiate the function signature. If substitution for the return type gets executed first, causing a simple substitution failure, the compiler will discard this function template when computing the overload set and search for other viable