以不同的枚举类型为输入的功能如何?
我面临以下问题。假设我有两个(或更多)类似这样的枚举类:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种选择是编写几个过载功能,每个功能都处理不同的枚举类型。例如:
然后,超载分辨率将选择正确的函数来致电:
One option would be to write several overloaded functions, each of which handles a different enumerated type. For example:
Overload resolution will then pick the right function to call:
我同意关于使用过载的273K和TemplateTypEdef。
但是,如果您对拥有案例处理逻辑的模板设定为一个地方,则可以这样做:
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: