sfinae

Checking whether a function (not a method) exists in c++11 via templates

耗尽温柔 提交于 2019-12-01 08:03:56
So with SFINAE and c++11, it is possible to implement two different template functions based on whether one of the template parameters can be substituted. For example struct Boo{ void saySomething(){ cout << "Boo!" << endl; } }; template<class X> void makeitdosomething(decltype(&X::saySomething), X x){ x.saySomething(); } template<class X> void makeitsaysomething(int whatever, X x){ cout << "It can't say anything!" << endl; } int main(){ makeitsaysomething(3); makeitsaysomething(Boo()); } or something along that line. My question is.. how does one do the same thing, but for non-member

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

对着背影说爱祢 提交于 2019-12-01 06:23:22
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? 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 Fallback::*, &U::x>*); template<typename U> static yes test(...); static const bool value = sizeof(test

How to SFINAE out non-containers parameters

妖精的绣舞 提交于 2019-12-01 05:19:15
I have a template function that I want to enable only for standard containers (or containers compatible with standard containers, which at least provide a begin() member function). I'm SFINAE-ing out non-containers in the following way: template<typename Container> typename Container::value_type f(const Container& c, typename std::enable_if< std::is_same< decltype(*c.begin()), typename Container::value_type >::value >::type* = nullptr) { // implementation here } The std::is_same and decltype don't look too elegant. Is there any better way of doing this? PS: I need the SFINAE here because I

Using std::enable_if with anonymous type parameters

两盒软妹~` 提交于 2019-12-01 04:57:36
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<int>(); } The compiler says: 7:3: error: redefinition of 'template<class T, class> T foo()' 4:3: note:

Template detects if T is pointer or class

心已入冬 提交于 2019-12-01 04:29:19
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? You can use a combination of std::is_pointer and std::enable_if : #include <type_traits> #include <iostream> class MyClass

SFINAE and detecting if a C++ function object returns void

旧巷老猫 提交于 2019-12-01 04:05:01
I've read the various authorities on this, include Dewhurst and yet haven't managed to get anywhere with this seemingly simple question. What I want to do is to call a C++ function object , (basically, anything you can call, a pure function or a class with ()), and return its value, if that is not void, or "true" otherwise. using std: struct Foo { void operator()() { cout << "Foo/"l; } }; struct Bar { bool operator()() { cout << "Bar/"; return true; } }; Foo foo; Bar bar; bool baz() { cout << "baz/"; return true; } void bang() { cout << "bang/"; } const char* print(bool b) { cout << b ? "true/

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

∥☆過路亽.° 提交于 2019-12-01 02:58:23
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 A { }; int main() { A a; My<int> m; My<A> ma; m.f(1); ma.f(a); return 0; } I'm getting an error: error

How to SFINAE out non-containers parameters

旧街凉风 提交于 2019-12-01 02:44:15
问题 I have a template function that I want to enable only for standard containers (or containers compatible with standard containers, which at least provide a begin() member function). I'm SFINAE-ing out non-containers in the following way: template<typename Container> typename Container::value_type f(const Container& c, typename std::enable_if< std::is_same< decltype(*c.begin()), typename Container::value_type >::value >::type* = nullptr) { // implementation here } The std::is_same and decltype

SFINAE differentiation between signed and unsigned

二次信任 提交于 2019-11-30 19:25:37
I have functions for converting different arithmetic types to a half precision floating point type (just a uint16_t on the lowest level) and I have different functions for integer and floating point source types, using SFINAE and std::enable_if : template<typename T> uint16_t to_half(typename std::enable_if< std::is_floating_point<T>::value,T>::type value) { //float to half conversion } template<typename T> uint16_t to_half(typename std::enable_if< std::is_integral<T>::value,T>::type value) { //int to half conversion } These are called internally from a universal templated constructor by

Is it possible to check for existence of member templates just by an identifier?

痞子三分冷 提交于 2019-11-30 18:16:24
Can we detect member function template , variable template , class / struct / union template or alias template without knowing amount, or nature of template / non-template parameters? When I try to think about this, nothing really comes to my mind. But let's have structure with member function template: struct foo { // Really random. Let's assume we don't know this declaration, just the name "bar" template <class T, std::size_t N, class... Args> void bar(T a, T b, T(&c)[N], Args const& ...); }; How do I check if the foo::bar template exists? Instantiation-based type traits don't apply here,