Home > Software design >  Function which takes different enum classes types as input, how?
Function which takes different enum classes types as input, how?

Time:06-25

I am facing the following problem. Supposing I have two (or more) enum classes like these:

enum class CURSOR { ON, OFF };
enum class ANSI { ON, OFF };

I am trying to implement a (template) function called OPTION able to do something like this:

OPTION( CURSOR::ON );
OPTION( ANSI::ON );

I tried implementing it in this way:

template <typename T>
inline void OPTION( const T& opt )
 {
  if( opt == CURSOR::ON ) //do something...;
  else if( opt == CURSOR::OFF ) //do something...; 
  else if( opt == ANSI::ON ) //do something...;
  else if( opt == ANSI::OFF ) //do something...;
 }

But it gives me the following error if I try to compile the two previously defined lines of code:

examples/../include/manipulators/csmanip.hpp: In instantiation of 'void osm::OPTION(const T&) [with T = CURSOR]':
examples/manipulators.cpp:190:14:   required from here
examples/../include/manipulators/csmanip.hpp:88:18: error: no match for 'operator==' (operand types are 'const osm::CURSOR' and 'osm::ANSI')
   88 |     else if( opt == ANSI::ON ) enableANSI();
      |              ~~~~^~~~~~~~~~~
examples/../include/manipulators/csmanip.hpp:88:18: note: candidate: 'operator==(osm::ANSI, osm::ANSI)' (built-in)
examples/../include/manipulators/csmanip.hpp:88:18: note:   no known conversion for argument 1 from 'const osm::CURSOR' to 'osm::ANSI'
examples/../include/manipulators/csmanip.hpp:88:18: note: candidate: 'operator==(osm::CURSOR, osm::CURSOR)' (built-in)
examples/../include/manipulators/csmanip.hpp:88:18: note:   no known conversion for argument 2 from 'osm::ANSI' to 'osm::CURSOR'
examples/../include/manipulators/csmanip.hpp:89:18: error: no match for 'operator==' (operand types are 'const osm::CURSOR' and 'osm::ANSI')
   89 |     else if( opt == ANSI::OFF ) disableANSI();

Please ignore the fact that I defined them in a namespace osm in my code.

Do you know what is the problem and if you know how to build a function which works for this task? Thanks.

CodePudding user response:

One option would be to write several overloaded functions, each of which handles a different enumerated type. For example:

void OPTION(CURSOR c) {
    switch (c) {
        case CURSOR::ON:  /* turn cursor on */;  break;
        case CURSOR::OFF: /* turn cursor off */; break;
    }
}

void OPTION(ANSI a) {
    switch (a) {
        case ANSI::ON:  /* turn ANSI on */;  break;
        case ANSI::OFF: /* turn ANSI off */; break;
    }
}

Overload resolution will then pick the right function to call:

OPTION(CURSOR::OFF); // Calls first function
OPTION(ANSI::ON);    // Calls second function   

CodePudding user response:

I agree with 273K and templatetypedef about using overloads instead.

However, if you have your heart set on having a template with the case handling logic all in one place, you can do it this way:

#include <iostream>
#include <type_traits>

using std::cout;

enum class CURSOR { ON, OFF };
enum class ANSI { ON, OFF };

template <typename T>
void OPTION(T opt) {
    if constexpr (std::is_same_v<T, CURSOR>) {
        if (opt == CURSOR::ON) cout << "cursor is on\n";
        else if (opt == CURSOR::OFF) cout << "cursor is off\n";
    } else if constexpr (std::is_same_v<T, ANSI>) {
        if (opt == ANSI::ON) cout << "ANSI is on\n";
        else if (opt == ANSI::OFF) cout << "ANSI is off\n";
    }
}

int main() {
    OPTION( CURSOR::ON );
    OPTION( ANSI::ON );
}
  • Related