sfinae

Detecting constexpr with SFINAE

大城市里の小女人 提交于 2019-11-26 20:24:09
I'm working on upgrading some C++ code to take advantage of the new functionality in C++11. I have a trait class with a few functions returning fundamental types which would most of the time, but not always, return a constant expression. I would like to do different things based on whether the function is constexpr or not. I came up with the following approach: template<typename Trait> struct test { template<int Value = Trait::f()> static std::true_type do_call(int){ return std::true_type(); } static std::false_type do_call(...){ return std::false_type(); } static bool call(){ return do_call(0

Detect operator support with decltype/SFINAE

吃可爱长大的小学妹 提交于 2019-11-26 19:46:31
问题 A (somewhat) outdated article explores ways to use decltype along with SFINAE to detect if a type supports certain operators, such as == or < . Here's example code to detect if a class supports the < operator: template <class T> struct supports_less_than { static auto less_than_test(const T* t) -> decltype(*t < *t, char(0)) { } static std::array<char, 2> less_than_test(...) { } static const bool value = (sizeof(less_than_test((T*)0)) == 1); }; int main() { std::cout << std::boolalpha <<

using SFINAE for template class specialisation

末鹿安然 提交于 2019-11-26 19:41:51
问题 suppose I have these declarations template<typename T> class User; template<typename T> class Data; and want to implement User<> for T = Data<some_type> and any class derived from Data<some_type> but also allow for other specialisations defined elsewhere. If I didn't already have the declaration of the class template User<> , I could simply template<typename T, typename A= typename std::enable_if<is_Data<T>::value>::type> class User { /*...*/ }; where template<template<typename> data>> struct

Is substitution performed on a variadic parameter pack type if the pack is empty?

孤者浪人 提交于 2019-11-26 17:02:55
问题 Consider the following program: #include <type_traits> enum class dummy {}; template <typename T> using EnableIf = typename std::enable_if<T::value, dummy>::type; template <typename T> using DisableIf = typename std::enable_if<!T::value, dummy>::type; template <typename T> struct dependent_true_type : std::true_type {}; template <typename T, EnableIf<dependent_true_type<T>>...> std::true_type f(); template <typename T, DisableIf<dependent_true_type<T>>...> std::false_type f(); static_assert

How to check whether operator== exists?

╄→гoц情女王★ 提交于 2019-11-26 14:33:17
I am trying to create an example, which would check the existence of the operator== (member or, non-member function). To check whether a class has a member operator== is easy, but how to check whether it has a non-member operator== ? This is what I have to far : #include <iostream> struct A { int a; #if 0 bool operator==( const A& rhs ) const { return ( a==rhs.a); } #endif }; #if 1 bool operator==( const A &l,const A &r ) { return ( l.a==r.a); } #endif template < typename T > struct opEqualExists { struct yes{ char a[1]; }; struct no { char a[2]; }; template <typename C> static yes test(

SFINAE works differently in cases of type and non-type template parameters

泄露秘密 提交于 2019-11-26 14:27:27
问题 Why does this code work: template< typename T, std::enable_if_t<std::is_same<T, int>::value, T>* = nullptr> void Add(T) {} template< typename T, std::enable_if_t<!std::is_same<T, int>::value, T>* = nullptr> void Add(T) {} and can correctly distinguish between these two calls: Add(1); Add(1.0); while the following code if compiled results in the redefinition of Add() error? template< typename T, typename = typename std::enable_if<std::is_same<T, int>::value, T>::type> void Add(T) {} template<

Using SFINAE to check for global operator<<?

泪湿孤枕 提交于 2019-11-26 12:31:11
问题 I want to have several overloaded, global to_string() functions that take some type T and convert it to its string representation. For the general case, I want to be able to write: template<typename T,class OutputStringType> inline typename enable_if<!std::is_pointer<T>::value && has_insertion_operator<T>::value, void>::type to_string( T const &t, OutputStringType *out ) { std::ostringstream o; o << t; *out = o.str(); } My implementation of has_insertion_operator so far is: struct sfinae_base

Can SFINAE detect private access violations?

余生颓废 提交于 2019-11-26 11:26:45
问题 I wonder whether if i test for some member of a class and the member is private what will sfinae respond? Will it error out hard or will it say ok or will it error out in the sfinae way? 回答1: Yes. EDIT: C++11 Standard quote from §14.8.2 [temp.deduct] 8/ If a substitution results in an invalid type or expression, type deduction fails. An invalid type or expression is one that would be ill-formed if written using the substituted arguments. [ Note: Access checking is done as part of the

Metaprograming: Failure of Function Definition Defines a Separate Function

强颜欢笑 提交于 2019-11-26 11:23:42
In this answer I define a template based on the type's is_arithmetic property: template<typename T> enable_if_t<is_arithmetic<T>::value, string> stringify(T t){ return to_string(t); } template<typename T> enable_if_t<!is_arithmetic<T>::value, string> stringify(T t){ return static_cast<ostringstream&>(ostringstream() << t).str(); } dyp suggests that rather than the is_arithmetic property of the type, that whether to_string is defined for the type be the template selection criteria. This is clearly desirable, but I don't know a way to say: If std::to_string is not defined then use the

What exactly is the “immediate context” mentioned in the C++11 Standard for which SFINAE applies?

纵然是瞬间 提交于 2019-11-26 11:22:31
Paragraph 14.8.2/8 of the C++11 Standard specifies the conditions under which a substitution failure shall or shall not result in a "hard" compilation error (thereby causing compilation to fail) or in a "soft" error which would just cause the compiler to discard a template from a set of candidates for overload resolution (without making compilation fail and enabling the well-known SFINAE idiom): If a substitution results in an invalid type or expression, type deduction fails. An invalid type or expression is one that would be ill-formed if written using the substituted arguments. [ Note: