sfinae

SFINAE failing with enum template parameter

Can someone explain the following behaviour (I'm using Visual Studio 2010). header: #pragma once #include <boost\utility\enable_if.hpp> using boost::enable_if_c; enum WeekDay {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}; template<WeekDay DAY> typename enable_if_c< DAY==SUNDAY, bool >::type goToWork() {return fal...

Detect if class has overloaded function fails on Comeau compiler

Hi Everyone, I'm trying to use SFINAE to detect if a class has an overloaded member function that takes a certain type. The code I have seems to work correctly in Visual Studio and GCC, but does not compile using the Comeau online compiler. Here is the code I'm using: #include <stdio.h> //Comeau doesnt' have boost, so define our own...

If the address of a function can not be resolved during deduction, is it SFINAE or a compiler error?

In C++0x SFINAE rules have been simplified such that any invalid expression or type that occurs in the "immediate context" of deduction does not result in a compiler error but rather in deduction failure (SFINAE). My question is this: If I take the address of an overloaded function and it can not be resolved, is that failure in the im...

Partial template specialization: matching on properties of specialized template parameter

template <typename X, typename Y> class A { // Use Y::Q, a useful property, not used for specialization. }; enum Property {P1,P2}; template <Property P> class B {}; class C {}; Is there any way to define a partial specialization of A such that A<C, B<P1> > would be A's normal template, but A<C, B<P2> > would be the specialization? ...

boost::enable_if class template method

I got class with template methods that looks at this: struct undefined {}; template<typename T> struct is_undefined : mpl::false_ {}; template<> struct is_undefined<undefined> : mpl::true_ {}; template<class C> struct foo { template<class F, class V> typename boost::disable_if<is_undefined<C> >::type apply...

does sfinae instantiates a function body?

I want to detect existence of a specific member function for a class, using the usual SFINAE trick. template<typename T> struct has_alloc { template<typename U,U x> struct dummy; template<typename U> static char test(dummy<void* (U::*)(std::size_t),&U::allocate>*); template<typename U> static char (&test(...))[...

Catching functors using SFINAE in structure partial specialisation

Hi, For some complicated reason, I want to convert any supported type T (coming from a template) to a list of types I have chosen. For this, I tried using a template structure named "Convert". For example: Convert<short>::type should be int Convert<int>::type should be int Convert<char>::type should be int Convert<float>::type should b...

Explain C++ SFINAE to a Python Guy

What is SFINAE in C++? Can you please explain it in words understandable to a Python programmer? Also, what concept in Python does SFINAE in C++ correspond to? ...

SFINAE: some failures more equal than others?

I'm trying to use SFINAE to distinguish a class that has a member called 'name'. I set things up in what seems to be the standard pattern but it's not working -- instead of silently ignoring the 'failed' substitution, the compiler produces an error. I'm sure I've run up against some template substitution rule, I'd be grateful if someon...

Can I use a SFINAE test in a control flow statement?

I have an SFINAE test for checking if an class has a function. The test works correctly, but I get compiler errors when I try to use it in an if statement. //SFINAE test for setInstanceKey() template <typename K> class HasSetInstanceKey { template <typename C> static char test( typeof(&C::setInstanceKey) ); template <typen...

use sfinae to test namespace members existence

Hello, I was trying to figure out if it is possible to use sfinae to test namespace member existence. Google is rather silent about it. I've tried the following code, but it fails. namespace xyz{ struct abc{}; } struct abc{}; struct test_xyz{ typedef char yes; typedef struct{ char a[2]; } no; template <class C> static yes test(...

Why should the member function declarations of a class template be all well-formed?

OK, suppose I want to check whether the template parameter has a nested type/typedef XYZ. template <class T> struct hasXZY { typedef char no; typedef struct { char x[2]; } yes; template <class U> static yes f(typename U::XYZ*); template <class /*U*/> static no f(...); enum {value = sizeof(f<T>(0))=...

Concept checking of static member variables compile error on gcc

I'm trying to apply the technique described in http://www.drdobbs.com/tools/227500449 With the sample code below, I expect the output: 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 And this is indeed what happens if I compile using clang. But with gcc, this code gives the following errors: junk.cpp: In instantiation of ‘const bool has_fo...

method compile time assertion; still not working

I need a easy way to assert inside a template that a template parameter implements a method (or one of its parent classes). I've read Concept check library but is hard to find an easy example to do simple checks like this one. I've tried to follow other posts (like this one and this other one), which i've modified so i can make it gener...