sfinae

How can I add reflection to a C++ application?

I'd like to be able to introspect a C++ class for its name, contents (i.e. members and their types) etc. I'm talking native C++ here, not managed C++, which has reflection. I realise C++ supplies some limited information using RTTI. Which additional libraries (or other techniques) could supply this information? ...

Is there a Technique in C++ to know if a class has a member function of a given signature

Hi, I'm asking for a template trick to detect if a class has a specific member function of a given signature. The problem is similar to the one cited here http://www.gotw.ca/gotw/071.htm but not the same: in the item of Sutter's book he answered to the question that a class C MUST PROVIDE a member function with a particular signature, ...

Is it possible to write a C++ template to check for a function's existence?

Is it possible to write a C++ template that changes behavior depending on if a certain member function is defined on a class? Here's a simple example of what I would want to write: template<class T> std::string optionalToString(T* obj) { if (FUNCTION_EXISTS(T->toString)) return obj->toString(); else return "toSt...

Using SFINAE to detect POD-ness of a type in C++

The original title here was Workaround for SFINAE bug in VS2005 C++ This is tentative use of SFINAE to make the equivalent for the is_pod template class that exists in TR1 (In VS2005 there's no TR1 yet). It should have its value member true when the template parameter is a POD type (including primitive types and structs made of them) an...

C++ "smart" predicate for stl algorithm.

I need to designe predicate for stl algorithms such as find_if, count_if. namespace lib { struct Finder { Finder( const std::string& name ): name_( name ) { } template< typename TElement > bool operator( const TElement& element ) { return element.isPresent(...

SFINAE with invalid function-type or array-type parameters?

Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main() { char c[sizeof(f<void()>(0)) == 2]; } I expected it doing SFINAE and chosing the second overload, since substitution of T into T[1] yields void [1]() Which is an invalid type, of course. Adjustment of parameter...

C++ SFINAE examples?

I want to get into more template meta-programming. I know that SFINAE stands for "substitution failure is not an error." But can someone show me a good use for SFINAE? ...

How to detect whether there is a specific member variable in class?

For creating algorithm template function I need to know whether x or X (and y or Y) in class that is template argument. It may by useful when using my function for MFC CPoint class or GDI+ PointF class or some others. All of them use different x in them. My solution could be reduces to the following code: template<int> struct TT {typed...

Why do you sometimes need to write <typename T> instead of just <T> ?

I was reading the Wikipedia article on SFINAE and encountered following code sample: struct Test { typedef int Type; }; template < typename T > void f( typename T::Type ) {} // definition #1 template < typename T > void f( T ) {} // definition #2 void foo() { f< Test > ( 10 ); //call #1 f< int > ( 10 )...

Determine whether a class has a function

Using a trick (described by Olivier Langlois), I can determine whether a class has a type defined: template<typename T> struct hasType { template<typename C> static char test( typename C::Type ); template<typename C> static char* test(...); enum{ Value= sizeof(test<T>(0))==1 }; }; I can also determine whether a class has a...

How to call a templated function if it exists, and something else otherwise?

I want to do something like template <typename T> void foo(const T& t) { IF bar(t) would compile bar(t); ELSE baz(t); } I thought that something using enable_if would do the job here, splitting up foo into two pieces, but I can't seem to work out the details. What's the simplest way of achieving this? ...

Why does SFINAE not apply to this?

I'm writing some simple point code while trying out Visual Studio 10 (Beta 2), and I've hit this code where I would expect SFINAE to kick in, but it seems not to: template<typename T> struct point { T x, y; point(T x, T y) : x(x), y(y) {} }; template<typename T, typename U> struct op_div { typedef decltype(T() / U()) type; ...

How can I have optional default constructor?

This class: template <class T> struct A { A() : t(T()) { } A(const T& t_) : t(t_) { } T t; }; won't compile if T doesn't have default constructor. This one: template <class T> struct A { A(const T& t_) : t(t_) { } T t; }; won't have default constructor even if T has default constructor. I want to have both - If...

SFINAE to check for inherited member functions

Using SFINAE, i can detect wether a given class has a certain member function. But what if i want to test for inherited member functions? The following does not work in VC8 and GCC4 (i.e. detects that A has a member function foo(), but not that B inherits one): #include <iostream> template<typename T, typename Sig> ...

SFINAE canAdd template problem

I'm trying tow write a SFINAE template to determine whether two classes can be added together. This is mostly to better understand how SFINAE works, rather than for any particular "real world" reason. So what I've come up with is #include <assert.h> struct Vec { Vec operator+(Vec v ); }; template<typename T1, typename T2> struct Ca...

SFINAE + sizeof = detect if expression compiles

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> sta...

How to determine whether a class has a particular templated member function?

I was wondering if it's possible to extend the SFINAE approach to detecting whether a class has a certain member function (as discussed here: "Is there a Technique in C++ to know if a class has a member function of a given signature?" http://stackoverflow.com/questions/87372/is-there-a-technique-in-c-to-know-if-a-class-has-a-member-func...

Why this works (Templates, SFINAE). C++

Hi guys, referring to yesterday's post, this woke me up this morning. Why does this actually work? As long as the function test is concerned, this function has no body so how can it perform anything? I want to know why and how this works? I'm REALLY interested to see your answers. template<typename T> class IsClassT { private: ...

Declare module name of classes for logging

I currently am adding some features to our logging-library. One of these is the possibility to declare a module-name for a class that automatically gets preprended to any log-messages writing from within that class. However, if no module-name is provided, nothing is prepended. Currently I am using a trait-class that has a static function...

Multiple SFINAE rules

Hi everyone, After reading the answer to this question, I learned that SFINAE can be used to choose between two functions based on whether the class has a certain member function. It's the equivalent of the following, just that each branch in the if statement is split into an overloaded function: template<typename T> void Func(T& arg)...