[英]force a strongly typed enum to participate in template overload resolution
根据这个问题 - 使用强类型枚举的模板参数推导- 似乎具有挑战性 - 如果不是不可能的话 - 让强类型枚举器参与重载决议。
如果我们有以下
#include <string>
#include <iostream>
void ExternalFunction(const std::string& tag, int value)
{
std::cout << tag << " - (int) " << value << std::endl;
}
void ExternalFunction(const std::string& tag, double value)
{
std::cout << tag << " - (double) " << value << std::endl;
}
void ExternalFunction(const std::string& tag, const std::string& value)
{
std::cout << tag << " - (string) " << value << std::endl;
}
class Wrapper
{
public:
virtual void DoSomething() = 0;
};
template <typename variable_type>
class VariableWrapper : public Wrapper
{
public:
VariableWrapper(variable_type variable) : variable(variable) {}
void DoSomething() override
{
ExternalFunction("tag", variable);
}
variable_type& variable;
};
template <typename enumerator, typename convert_to_string>
class EnumWrapper : public VariableWrapper<enumerator>
{
public:
EnumWrapper(enumerator& variable, convert_to_string encoder) : VariableWrapper<enumerator>(variable), encoder(encoder) {}
void DoSomething() override
{
ExternalFunction("tag", encoder(VariableWrapper<enumerator>::variable));
}
convert_to_string encoder;
};
enum class StronglyTyped
{
A,
B,
C
};
int main()
{
StronglyTyped e = StronglyTyped::A;
Wrapper* wrapper = new EnumWrapper(e, [](StronglyTyped S)->std::string{return "Test";});
wrapper->DoSomething();
}
如果我们尝试运行它 - http://coliru.stacked-crooked.com/a/d555c4e3300ab05d - 我们会得到错误
main.cpp: In instantiation of 'void VariableWrapper<variable_type>::DoSomething() [with variable_type = StronglyTyped]':
main.cpp:31:14: required from here
main.cpp:33:29: error: no matching function for call to 'ExternalFunction(const char [4], StronglyTyped&)'
33 | ExternalFunction("tag", variable);
| ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
main.cpp:4:6: note: candidate: 'void ExternalFunction(const string&, int)'
4 | void ExternalFunction(const std::string& tag, int value)
| ^~~~~~~~~~~~~~~~
main.cpp:4:51: note: no known conversion for argument 2 from 'StronglyTyped' to 'int'
4 | void ExternalFunction(const std::string& tag, int value)
| ~~~~^~~~~
main.cpp:9:6: note: candidate: 'void ExternalFunction(const string&, double)'
9 | void ExternalFunction(const std::string& tag, double value)
| ^~~~~~~~~~~~~~~~
main.cpp:9:54: note: no known conversion for argument 2 from 'StronglyTyped' to 'double'
9 | void ExternalFunction(const std::string& tag, double value)
| ~~~~~~~^~~~~
main.cpp:14:6: note: candidate: 'void ExternalFunction(const string&, const string&)'
14 | void ExternalFunction(const std::string& tag, const std::string& value)
| ^~~~~~~~~~~~~~~~
main.cpp:14:66: note: no known conversion for argument 2 from 'StronglyTyped' to 'const string&' {aka 'const std::__cxx11::basic_string<char>&'}
14 | void ExternalFunction(const std::string& tag, const std::string& value)
| ~~~~~~~~~~~~~~~~~~~^~~~~
是否可以让强类型枚举器参与重载决议? 我不想删除强类型 - 这确实删除了这个问题 - 因为我将不得不在多个枚举之间使用唯一的名称
编辑:我已经从问题中删除了 std::to_string 并相应地更新了代码,因为它被错误地关注。
您编写的代码不正确。
好的,所以EnumWrapper<>::DoSomething
覆盖了VariableWrapper<T>::DoSomething
。 所以它不会通过在任何EnumWrapper
对象上直接调用DoSomething
来调用。
但这并不意味着VariableWrapper<T>::DoSomething
不存在。 它确实存在,您仍然可以通过合格的调用来调用它。 因此,当您为某个特定T
实例化VariableWrapper<T>
,此模板类的成员函数必须有效。
并且VariableWrapper<T>::DoSomething
对任何不能用于直接调用ExternalFunction
T
无效。 这与具体的枚举无关; 这纯粹是关于你如何编写你的函数。
您应该改为使EnumWrapper
不继承自VariableWrapper
。 它们都可以从BaseWrapper
继承,但它们没有相互继承的业务。 或者,您可以使ExternalFunction
本身成为一个模板,该模板可以解决如何为任何给定类型执行任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.