Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: using std::enable_if for sf::Packet overloads for enums  (Read 2394 times)

0 Members and 1 Guest are viewing this topic.

iride

  • Jr. Member
  • **
  • Posts: 88
    • View Profile
    • Email
using std::enable_if for sf::Packet overloads for enums
« on: January 20, 2014, 08:28:30 pm »
I'm trying to write a function template for overloading enums with sf::Packet. What did I do wrong?

template <class Enum>
sf::Packet & operator<<(sf::Packet & packet, const typename std::enable_if<std::is_enum<Enum>::value, Enum>::type & t)
{
        return packet << static_cast<typename std::underlying_type<EnumT>::type>(t);
}

template <class Enum>
sf::Packet & operator>>(sf::Packet & packet, typename std::enable_if<std::is_enum<Enum>::value, Enum>::type & t)
{
        typename std::underlying_type<EnumT>::type i;
        packet >> i;
        t = static_cast<Enum>(i);
        return packet;
}

enum Month : sf::Int32
{
    January
}

//doesn't compile
sf::Packet packet;
packet<<January;
Month month;
packet>>month;
 

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: using std::enable_if for sf::Packet overloads for enums
« Reply #1 on: January 20, 2014, 10:09:46 pm »
Type deduction of parameters in function templates requires the type to appear in a non-dependent context. typename X::type however is a dependent type. This rule exists because otherwise, the compiler would have to perform an exhaustive search (it cannot know which template arguments lead to the result type).

That is why SFINAE is always implemented through an additional default function parameter, an additional template parameter or the return type -- but not one of the regular function parameters.

In this case, only option 2 and 3 work; for example, you can use the return type.
template <typename Enum>
typename std::enable_if<std::is_enum<Enum>::value, sf::Packet&>::type
  operator<< (sf::Packet& packet, Enum t)
{
        ...
}

template <typename Enum>
typename std::enable_if<std::is_enum<Enum>::value, sf::Packet&>::type
  operator>> (sf::Packet& packet, Enum& t)
{
        ...
}

By the way: Next time, please post the compiler message and the used compiler, and fix your code before posting (missing semicolon, wrong EnumT). It's a pity when it's not clear whether the problem arises from a simple typo or something more complex. Just copy-paste the code, do not type it here by hand.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

iride

  • Jr. Member
  • **
  • Posts: 88
    • View Profile
    • Email
Re: using std::enable_if for sf::Packet overloads for enums
« Reply #2 on: January 20, 2014, 10:45:17 pm »
Wow thank you so much. Never heard of SFINAE before.
Anyways I think this overload will be a good addition for SFML since enums are frequently used in packets.
Could this be added to SFML when SFML supports c++11?

 

anything