sfinae

While testing SFINAE, I found something that I don't think should work

爷,独闯天下 提交于 2019-12-11 06:55:50
问题 I found an interesting conditional function exclusion that I got from this site and while testing it I came across this: #include<type_traits> namespace detail { enum enabler {}; } template <int overload, typename Condition> using EnableIf = std::enable_if_t<Condition::value, detail::enabler>; template <typename T, EnableIf<0, std::is_same<T, int>>...> T twice(T t) { return 2 * t; } template <typename T, EnableIf<0, std::is_same<T, float>>...> T twice(T t) { return 2 * t; } int main() { twice

Sample function processing time decorator in c++11

我与影子孤独终老i 提交于 2019-12-11 04:06:18
问题 I am trying to write a template that will take any function and log its time and after some struggle with template syntax i have come up with below solution: template <typename Func, typename... Args> auto timeMyFunction(Func f, Args... args)-> typename std::enable_if<std::is_same<decltype(f(args...)),void>::value,void>::type { auto start = std::chrono::steady_clock::now(); f(args...); auto end = std::chrono::steady_clock::now(); std::chrono::duration<double> diff = end-start; std::cout <<

Disable default template and only use specialization through sfinae

拟墨画扇 提交于 2019-12-11 03:09:13
问题 Consider the following system: template<typename T> struct wrapper { operator T * () { return nullptr; } }; template<typename Ret, typename T> Ret func(T); template<> int func(float * in) { std::cout << "long"; } template<> long func(float * in) { std::cout << "int"; } The purpose of the wrapper is to allow it to decay to the type it is templated to (it is a wrapper around a buffer of the type). Moreover, i have a set of functions that are templated specializations of a template. This is to

Using std::enable_if on template function return type to exploit SFINAE - compilation error

天大地大妈咪最大 提交于 2019-12-11 02:28:26
问题 The following code #include <type_traits> struct CByteArray {}; struct HLVariant { HLVariant() {} HLVariant(const HLVariant&) {} HLVariant(const CByteArray&) {} }; template <typename T> inline typename std::enable_if<!std::is_pod<T>::value, CByteArray>::type serialize(const T& value) { return serialize(HLVariant(value)); } template <typename T> inline typename std::enable_if<std::is_pod<T>::value, CByteArray>::type serialize(const T& value) { return CByteArray(); } template <> inline

SFINAE: “enable_if cannot be used to disable this declaration”

淺唱寂寞╮ 提交于 2019-12-11 00:34:37
问题 Why can I not use enable_if in the following context? I'd like to detect whether my templated object has the member function notify_exit template <typename Queue> class MyQueue { public: auto notify_exit() -> typename std::enable_if< has_member_function_notify_exit<Queue, void>::value, void >::type; Queue queue_a; }; Initialised with: MyQueue<std::queue<int>> queue_a; I keep getting (clang 6): example.cpp:33:17: error: failed requirement 'has_member_function_notify_exit<queue<int, deque<int,

Select constructor through SFINAE in template arguments

主宰稳场 提交于 2019-12-10 20:26:30
问题 I'm trying to select a constructor through SFINAE as following: template<typename T> class MyClass { public: template<typename C, typename = std::enable_if_t<std::is_class<C>::value>> MyClass(C) { } template<typename C, typename = std::enable_if_t<std::is_pointer<C>::value>> MyClass(C) { } }; but the compiler complains with following error: error C2535: 'MyClass::MyClass(C)': member function already defined or declared without even instantiating the constructor. I worked out a working but

Type not inherited in SFINAE for multiple inheritance?

吃可爱长大的小学妹 提交于 2019-12-10 19:48:12
问题 I am using a SFINAE mechanism to deduce a type. Resolve<T>::type is deduced to T if class T doesn't contain yes and it's deduced to MyClass if it contains yes . class MyClass {}; template<typename> struct void_ { typedef void check; }; template<typename T, typename = void> struct Resolve { typedef T type; }; template<typename T> struct Resolve <T, typename void_<typename T::yes>::check> { typedef MyClass type; }; Now, I have the simple test classes as, struct B1 { typedef int yes; }; // 1

“Overloading” constructors with SFINAE

一个人想着一个人 提交于 2019-12-10 19:42:06
问题 Why does the the following attempt at overloading the constructor Foo::Foo fail? Also, I'd appreciate alternatives/workarounds #include <vector> #include <type_traits> namespace xyz { struct MemoryManager{}; template<typename T , typename Alloc=MemoryManager> class vector { }; } template<typename T, template<typename,typename> class V , typename A> struct Foo { template<typename U = T , typename Dummy = typename std::enable_if< std::is_same<std::vector<U>, V<U,A> >::value >::type > Foo() //

Is there a generic way to negate a decltype condition with SFINAE?

微笑、不失礼 提交于 2019-12-10 18:08:32
问题 I have a dozen functions or so that take two parameters: a generic, and a specific type. E.g.: template <class A, class B> void foo(A& a, B& b) { cout << "generic fallback" << endl; } template <class A> void foo(A& a, int &i) { cout << "generic int" << endl; } template <class A> void foo(A& a, string& s) { cout << "generic str" << endl; } I want to create an overload which will get called whenever A is an instance of a particular struct[1]. The best I came up with so far was: struct mine {

Metaprograming: Failure of Function Definition Defines a Separate Function

大憨熊 提交于 2019-12-10 18:07:39
问题 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