不同返回类型的模式匹配可以通过使用重载函数或者使用模板函数来实现。以下是两种解决方法的示例代码:
#include
void processInt(int num) {
std::cout << "Processing integer: " << num << std::endl;
}
void processFloat(float num) {
std::cout << "Processing float: " << num << std::endl;
}
void processString(const std::string& str) {
std::cout << "Processing string: " << str << std::endl;
}
int main() {
int intValue = 10;
float floatValue = 3.14;
std::string stringValue = "Hello";
processInt(intValue);
processFloat(floatValue);
processString(stringValue);
return 0;
}
#include
template
void process(T value) {
std::cout << "Processing value: " << value << std::endl;
}
int main() {
int intValue = 10;
float floatValue = 3.14;
std::string stringValue = "Hello";
process(intValue);
process(floatValue);
process(stringValue);
return 0;
}
这两种方法都可以实现不同返回类型的模式匹配,具体选择哪种方法取决于具体的场景和需求。