Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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> 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 Nobel prize? ;)

EDIT: I made the code simpler to understand and easier to adapt with two global function template declarations lvalue_of_type and rvalue_of_type.

like image 358
fredoverflow Avatar asked Jan 24 '10 16:01

fredoverflow


2 Answers

It's a well known technique, I'm afraid :-)

The use of a function call in the sizeof operator instructs the compiler to perform argument deduction and function matching, at compile-time, of course. Also, with a template function, the compiler also instantiates a concrete function from a template. However, this expression does does not cause a function call to be generated. It's well described in SFINAE Sono Buoni PDF.

Check other C++ SFINAE examples.

like image 63
mloskot Avatar answered Nov 05 '22 18:11

mloskot


It is just a combination of two well-known tricks. SFINAE says 'substitution failure is not an error' - that's exactly what you did. Using sizeof to let the compiler substitute template arguments into an expression without actually executing it is also common.

Sorry :-)

like image 1
Alexander Gessler Avatar answered Nov 05 '22 20:11

Alexander Gessler