以不同的枚举类型为输入的功能如何?

发布于 2025-02-10 20:07:18 字数 1800 浏览 1 评论 0原文

我面临以下问题。假设我有两个(或更多)类似这样的枚举类:

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

我正在尝试实现一个称为option能够执行此类操作的(模板)函数:

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

我尝试以这种方式实现它:

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

但是它给出了如果我尝试编译以前定义的代码行:

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();

请忽略以下事实:我在代码中以命名空间osm定义它们的事实。

您知道问题是什么吗?如果您知道如何构建适用于此任务的函数?谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人间不值得 2025-02-17 20:07:18

一种选择是编写几个过载功能,每个功能都处理不同的枚举类型。例如:

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

然后,超载分辨率将选择正确的函数来致电:

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

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   
川水往事 2025-02-17 20:07:18

我同意关于使用过载的273K和TemplateTypEdef。

但是,如果您对拥有案例处理逻辑的模板设定为一个地方,则可以这样做:

#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 );
}

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 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文