sfinae

specialize a template class constructor

[亡魂溺海] 提交于 2019-12-10 10:11:46
问题 I want to specialize a template class constructor: If type is int default value is 50 and -50 . and if it's float default should be 0.5 and -0.5 . My code is : #include <iostream> #include <limits> #include <type_traits> template<typename T> class Foo{ public: template<typename = typename std::enable_if< std::is_integral<T>::value&& !std::is_floating_point<T>::value>::type> Foo(T value1 = 50, T value2 = -50) :value1_(value1), value2_(value2){} template<typename = typename std::enable_if< std:

Use SFINAE to detect the existence of a templated member function

旧街凉风 提交于 2019-12-10 03:52:13
问题 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() {}

Make std's data-structure use my existing non-static hash function “hashCode()” by default

爱⌒轻易说出口 提交于 2019-12-10 02:48:58
问题 I have a moderate-size codebase (>200 .cpp ) that use a function hashCode() to return hash number:- class B01{ //a class //..... complex thing .... public: size_t hashCode(){ /* hash algorithm #H01 */} }; class B02{ //just another unrelated class //..... complex thing .... public: size_t hashCode(){/* #H02 */} //This is the same name as above }; I have used it in various locations, e.g. in my custom data-structure. It works well. Now, I want to make the hash algorithm recognized by std:: data

SFINAE template specialization precedence

五迷三道 提交于 2019-12-10 01:33:49
问题 #include <iostream> #include <array> #include <vector> template <typename T, typename SFINAE=void> struct trait; template <typename T> struct trait<T, decltype( std::declval<const T&>().begin(), std::declval<const T&>().end(), void() )> { static const char* name() { return "Container"; } }; template <typename T, std::size_t N> struct trait<std::array<T,N>> { static const char* name() { return "std::array"; } }; int main(int argc, char* argv[]) { std::cout << trait<std::vector<int>>::name() <<

SFINAE and noexcept specifier

纵饮孤独 提交于 2019-12-09 16:14:11
问题 Does an expression in noexcept specifier's parentheses participate in SFINAE during overload resolution of function templates? I want to make an wrapper for aggregates and want the std::is_constructible predicate to work properly for it: template< typename type > struct embrace : type { template< typename ...arguments > embrace(arguments &&... _arguments) noexcept(noexcept(type{std::forward< arguments >(_arguments)...})) : type{std::forward< arguments >(_arguments)...} // braces { ; } }; int

Why is a partial class template specialization on a matching template class ambiguous with another partial specialization without the template match?

蓝咒 提交于 2019-12-09 12:46:18
问题 The question may be too hard to describe in on sentence in the title, but here is a minimal example: #include <iostream> #include <type_traits> template <class T, class U, class Enabler> struct my_trait : std::false_type {}; template <class T, class U> struct my_trait<T, U, std::enable_if_t<std::is_same<T, U>::value>> : std::true_type {}; template <class T> class temped {}; template <class T> struct my_trait<temped<T>, temped<T>, void> : std::false_type {}; template <class T, class U> using

SFINAE to test a free function from another namespace

空扰寡人 提交于 2019-12-09 10:15:36
问题 I was trying to come up with a hack to test if std::isnan is defined without special casing compilers in the preprocessor, and came up with the following, which I was expecting to work fine. #include <cmath> #include <type_traits> namespace detail { using namespace std; struct dummy {}; void isnan(dummy); //bool isnan(float); // Just adding this declaration makes it work! template <typename T> struct is_isnan_available { template <typename T1> static decltype(isnan(T1())) test(int); template

Using std::enable_if with anonymous type parameters

烂漫一生 提交于 2019-12-09 03:13:51
问题 I try to use std::enable_if with an unused and unnamed type parameter, in order to not distort the return type. However, the following code does not compile. #include <iostream> template <typename T, typename = std::enable_if_t<!std::is_integral<T>::value>> T foo() { std::cout << "non-integral" << std::endl; return T(); } template <typename T, typename = std::enable_if_t<std::is_integral<T>::value>> T foo() { std::cout << "integral" << std::endl; return T(); } int main() { foo<float>(); foo

How to use enable_if to enable member functions based on template parameter of class

╄→гoц情女王★ 提交于 2019-12-09 02:49:04
问题 In code: template<class T> struct is_builtin { enum {value = 0}; }; template<> struct is_builtin<char> { enum {value = 1}; }; template<> struct is_builtin<int> { enum {value = 1}; }; template<> struct is_builtin<double> { enum {value = 1}; }; template<class T> struct My { typename enable_if<is_builtin<T>::value,void>::type f(T arg) { std::cout << "Built-in as a param.\n"; } typename enable_if<!is_builtin<T>::value,void>::type f(T arg) { std::cout << "Non - built-in as a param.\n"; } }; struct

MSVC 2013 'type' : is not a member of 'std::enable_if<false,void>

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 19:23:19
问题 My SFINAE code using std::enable_if compiles in GCC & Clang, but not in MSVC 2013. The code (also available on cpp.sh) is #include <iostream> #include <type_traits> template <typename T, typename ... AdditionalInputs> typename std::enable_if<sizeof...(AdditionalInputs) == 0, void>::type CallDoDataProcessing(T var) { std::cout << sizeof...(AdditionalInputs) << " additional inputs" << std::endl; } template <typename T, typename ... AdditionalInputs> typename std::enable_if<sizeof...