sfinae

use sfinae to test namespace members existence

不想你离开。 提交于 2019-11-30 15:01:44
I was trying to figure out if it is possible to use sfinae to test namespace member existence. Google is rather silent about it. I've tried the following code, but it fails. namespace xyz{ struct abc{}; } struct abc{}; struct test_xyz{ typedef char yes; typedef struct{ char a[2]; } no; template <class C> static yes test(xyz::C = xyz::C()); //lets assume it has default constructor template <class C> static no test(...); const bool has_abc = sizeof(test_xyz::test<abc>()) == sizeof(yes); }; Any idea why? Regards, No, that won't work. There is also no way to use SFINAE in such a way (this was last

Overload resolution between template members in base and derived classes

不想你离开。 提交于 2019-11-30 15:00:23
Microsoft compiler (Visual Studio 2017 15.2) rejects the following code: #include <type_traits> struct B { template<int n, std::enable_if_t<n == 0, int> = 0> void f() { } }; struct D : B { using B::f; template<int n, std::enable_if_t<n == 1, int> = 0> void f() { } }; int main() { D d; d.f<0>(); d.f<1>(); } The error is: error C2672: 'D::f': no matching overloaded function found error C2783: 'void D::f(void)': could not deduce template argument for '__formal' note: see declaration of 'D::f' Clang also rejects it: error: no matching member function for call to 'f' d.f<0>(); ~~^~~~ note:

Enable template function if class has specific member function

末鹿安然 提交于 2019-11-30 12:23:29
I wrote the following template function, which checks whether an arbitary container contains a specific element: template<template<class, class...> class container_t, class item_t, class... rest_t> bool contains(const container_t<item_t, rest_t...> &_container, const item_t &_item) { for(const item_t &otherItem : _container) { if(otherItem == _item) { return true; } } return false; } This works well for most containers. However for all kinds of sets (and maps) it is sub optimal since there we could use: template<template<class, class...> class set_t, class item_t, class... rest_t> bool

How to avoid this sentence is false in a template SFINAE?

回眸只為那壹抹淺笑 提交于 2019-11-30 11:53:00
问题 So I want to write an automatic != : template<typename U, typename T> bool operator!=(U&& u, T&& t) { return !( std::forward<U>(u) == std::forward<T>(t) ); } but that is impolite 1 . So I write // T() == U() is valid? template<typename T, typename U, typename=void> struct can_equal:std::false_type {}; template<typename T, typename U> struct can_equal< T, U, typename std::enable_if< std::is_convertible< decltype( std::declval<T>() == std::declval<U>() ), bool >::value >::type >: std::true_type

SFINAE check for operator+=

给你一囗甜甜゛ 提交于 2019-11-30 11:32:08
I'm trying to eliminate an overload from an overload set if operator+= is missing. I know how to check if T+T is legal : template<typename T, typename CheckTplusT = decltype(std::declval<T>() + std::declval<T>())> void foo(T a, T b, ...) { a = a + b; } but this doesn't work for += template<typename T, typename CheckTplusT = decltype(std::declval<T>() += std::declval<T>())> void foo(T a, T b, ...) { a += b; } Is this fixable by using another expression inside decltype or do I need another SFINAE construct? The reason I need this eliminated from the overload set is that it clashes with another

Using SFINAE to check if the type is complete or not [duplicate]

孤街醉人 提交于 2019-11-30 11:21:39
This question already has an answer here: How to write `is_complete` template? 7 answers Is it possible to check with SFINAE if the type is completely defined? E.g. template <class T> struct hash; template <> struct hash<int> {}; // is_defined_hash_type definition... enum Enum { A, B, C, D }; static_assert ( is_defined_hash_type<int> ::value, "hash<int> should be defined"); static_assert (! is_defined_hash_type<Enum>::value, "hash<Enum> should not be defined"); The solution should not modify the hash struct . You can make an is_complete type trait, using the fact that it is ill-formed to

What trait / concept can guarantee memsetting an object is well defined?

北城余情 提交于 2019-11-30 11:03:01
Let's say I have defined a zero_initialize() function: template<class T> T zero_initialize() { T result; std::memset(&result, 0, sizeof(result)); return result; } // usage: auto data = zero_initialize<Data>(); Calling zero_initialize() for some types would lead to undefined behavior 1, 2 . I'm currently enforcing T to verify std::is_pod . With that trait being deprecated in C++20 and the coming of concepts, I'm curious how zero_initialize() should evolve. What (minimal) trait / concept can guarantee memsetting an object is well defined? Should I use std::uninitialized_fill instead of std:

checking if a class inherits from any template instantiation of a template

限于喜欢 提交于 2019-11-30 10:01:21
I have written a small utility for testing whether or not a type has inherited some template instantiation of a specific template class, either directly or trough inheriting a class that inherits the template. This is accomplished with a SFINAE check using a template function accepting any template instantiation of the provided template and a fallback overload for the default case. #include <iostream> #include <type_traits> template<template<class> class T, class U> struct isDerivedFrom { static constexpr bool value = decltype(isDerivedFrom::test(U()))::value; private: template<class V> static

How to check if a template parameter is an iterator type or not?

假如想象 提交于 2019-11-30 08:20:48
问题 template<class T> struct is_iterator { static const bool value = ??? // What to write ??? }; int main() { assert(false == is_iterator<int>::value); assert(true == is_iterator<vector<int>::iterator>::value); assert(true == is_iterator<list<int>::iterator>::value); assert(true == is_iterator<string::iterator>::value); assert(true == is_iterator<char*>::value); // a raw pointer is also an iterator } The question is: How to make the five assert statements pass? 回答1: template<class T> struct is

'if' with template parameters or SFINAE is preferred?

青春壹個敷衍的年華 提交于 2019-11-30 08:05:25
Preferred is this: template<typename T> bool isNotZero(const T &a) { if (std::is_floating_point<T>::value) return abs(a) > std::numeric_limits<T>::epsilon(); else return a; } Or this:? template<typename T> std::enable_if<std::is_floating_point<T>::value, bool>::type isNotZero(const T &a) { return abs(a) > std::numeric_limits<T>::epsilon(); } template<typename T> std::enable_if<std::is_integral<T>::value, bool>::type isNotZero(const T &a) { return a; } I usually use the first type to avoid many versions of function. I believe it is exactly the same. The first version optimized in opcode stage