sfinae

Checking whether a template argument has a member function [duplicate]

十年热恋 提交于 2019-12-30 10:36:36
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Is it possible to write a C++ template to check for a function's existence? This is very similar to my earlier question. I want to check whether a template argument contains a member function or not. I tried this code similar to that in the accepted answer in my previous question. struct A { int member_func(); }; struct B { }; template<typename T> struct has_member_func { template<typename C> static char func(??

How to check with SFINAE if a member exists, without knowing the member's type?

倾然丶 夕夏残阳落幕 提交于 2019-12-30 10:12:29
问题 In pre-C++11 code, if I'm looking for a member variable whose type I don't know, how can I use SFINAE to check if the member exists? 回答1: Here's an example using Member detector idiom that you asked for: template<typename T> struct has_x { typedef char(&yes)[1]; typedef char(&no)[2]; // this creates an ambiguous &Derived::x if T has got member x struct Fallback { char x; }; struct Derived : T, Fallback { }; template<typename U, U> struct Check; template<typename U> static no test(Check<char

Template detects if T is pointer or class

会有一股神秘感。 提交于 2019-12-30 08:32:12
问题 Considering the following code: class MyClass { ... }; template <typename Object> class List { public: void insert(const Object & x) { // call when Object is MyClass } void insert(const Object & x) { // call when Object is MyClass* } } int main() { MyClass a; List<MyClass> lst; List<MyClass*> plst; lst.insert(a); plst.insert(new Myclass); return 0; } How to tell the compiler call different methods based on if the template is a class or a pointer? How to fix the code above? 回答1: You can use a

Enable template function if class has specific member function

心不动则不痛 提交于 2019-12-30 04:10:07
问题 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

SFINAE check for operator+=

浪尽此生 提交于 2019-12-30 03:59:07
问题 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

Static assertions and SFINAE

为君一笑 提交于 2019-12-30 02:11:51
问题 Consider this: template <typename T> struct hash { static_assert(false,"Not implemented."); }; struct unhashable {}; template <typename T> auto test(const T &t) -> decltype((*(hash<T> const *)nullptr)(t),int); void test(...); int main() { std::cout << std::is_same<decltype(test(std::declval<unhashable>())),void>::value; } Apart from obviously missing headers, should this compile? In other words, I am asking if the static assertion failure triggering inside a trailing decltype while deducing

SFINAE to make base template always result in error

心不动则不痛 提交于 2019-12-29 07:52:49
问题 So I'm designing a sort of my_numeric_cast function to limit the types of conversions available when using a framework I'm writing. It was pretty straight forward to do something like template<typename To, typename From> constexpr To my_numeric_cast(From); template<> constexpr float my_numeric_cast<float, int>(int i) { return i; } Which works, allowing only casting from ints to floats whenever the cast is used. And producing a linkage error whenever a cast not in the white list is attempted.

SFINAE to make base template always result in error

前提是你 提交于 2019-12-29 07:52:11
问题 So I'm designing a sort of my_numeric_cast function to limit the types of conversions available when using a framework I'm writing. It was pretty straight forward to do something like template<typename To, typename From> constexpr To my_numeric_cast(From); template<> constexpr float my_numeric_cast<float, int>(int i) { return i; } Which works, allowing only casting from ints to floats whenever the cast is used. And producing a linkage error whenever a cast not in the white list is attempted.

SFINAE + sizeof = detect if expression compiles

蹲街弑〆低调 提交于 2019-12-28 12:09:48
问题 I just found out how to check if operator<< is provided for a type. template<class T> T& lvalue_of_type(); template<class T> T rvalue_of_type(); template<class T> struct is_printable { template<class U> static char test(char(*)[sizeof( lvalue_of_type<std::ostream>() << rvalue_of_type<U>() )]); template<class U> static long test(...); enum { value = 1 == sizeof test<T>(0) }; typedef boost::integral_constant<bool, value> type; }; Is this trick well-known, or have I just won the metaprogramming

How to test whether class B is derived from template family of classes

守給你的承諾、 提交于 2019-12-28 04:20:10
问题 How to test at compile time whether class B is derived from std::vector? template<class A> struct is_derived_from_vector { static const bool value = ????; }; How to test at compile time whether class B is derived from template family? template<class A, template< class > class Family> struct is_derived_from_template { static const bool value = ????; }; Using: template<class T> struct X {}; struct A : X<int> {} struct B : std::vector<char> {} struct D : X<D> {} int main() { std::cout << is