SFINAE failing with enum template parameter

Posted by zeroes00 on Stack Overflow See other posts from Stack Overflow or by zeroes00
Published on 2010-05-04T08:31:17Z Indexed on 2010/05/04 8:38 UTC
Read the original article Hit count: 369

Filed under:
|
|

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 false;}

template<WeekDay DAY>
typename enable_if_c< DAY!=SUNDAY, bool >::type goToWork()  {return true;}

source:

bool b = goToWork<MONDAY>();

compiler this gives

error C2770: invalid explicit template argument(s) for 'enable_if_c<DAY!=6,bool>::type goToWork(void)'  

and

error C2770: invalid explicit template argument(s) for 'enable_if_c<DAY==6,bool>::type goToWork(void)'

But if I change the function template parameter from the enum type WeekDay to int, it compiles fine:

template<int DAY>
typename enable_if_c< DAY==SUNDAY, bool >::type goToWork()  {return false;}

template<int DAY>
typename enable_if_c< DAY!=SUNDAY, bool >::type goToWork()  {return true;}

Also the normal function template specialization works fine, no surprises there:

template<WeekDay DAY>  bool goToWork()          {return true;}
template<>             bool goToWork<SUNDAY>()  {return false;}

To make things even weirder, if I change the source file to use any other WeekDay than MONDAY or TUESDAY, i.e. bool b = goToWork<THURSDAY>(); the error changes to this:

error C2440: 'specialization' : cannot convert from 'int' to 'const WeekDay'  
Conversion to enumeration type requires an explicit cast (static_cast, C-style cast or function-style cast)

© Stack Overflow or respective owner

Related posts about sfinae

Related posts about enum