首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >函数,以不同的枚举类类型作为输入,如何?

函数,以不同的枚举类类型作为输入,如何?
EN

Stack Overflow用户
提问于 2022-06-24 16:07:55
回答 2查看 111关注 0票数 2

我面临以下问题。假设我有两个(或更多)枚举类,如下所示:

代码语言:javascript
运行
复制
enum class CURSOR { ON, OFF };
enum class ANSI { ON, OFF };

我正在尝试实现一个名为OPTION的(模板)函数,它能够执行如下操作:

代码语言:javascript
运行
复制
OPTION( CURSOR::ON );
OPTION( ANSI::ON );

我试着用这种方式来实现它:

代码语言:javascript
运行
复制
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...;
 }

但是,如果我试图编译前面定义的两行代码,则会出现以下错误:

代码语言:javascript
运行
复制
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中定义了它们。

你知道问题出在哪里吗?如果你知道如何构建一个适合这个任务的函数,你知道吗?谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-06-24 16:42:05

我同意273 k和templatetypedef关于使用重载的说法。

但是,如果您一心想在一个地方拥有一个具有案例处理逻辑的模板,那么您可以这样做:

代码语言:javascript
运行
复制
#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 );
}
票数 5
EN

Stack Overflow用户

发布于 2022-06-24 16:15:35

一个选项是编写几个重载函数,每个函数处理不同的枚举类型。例如:

代码语言:javascript
运行
复制
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;
    }
}

然后,重载解析将选择正确的函数来调用:

代码语言:javascript
运行
复制
OPTION(CURSOR::OFF); // Calls first function
OPTION(ANSI::ON);    // Calls second function   
票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72746726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档