c++ - 我如何修改此函数,以便如果函数的参数是特定结构,它会返回具有更大整数的结构?

标签 c++ templates struct max

我必须修改这个 max 函数,如果它处理一个 struct Student,它将返回最高成绩。 这是代码:

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
typedef struct Student {
    char* name;
    int grade;
}Student;
template<typename  T>
T Max(T var1, T var2) {
    if (sizeof(T) == sizeof(Student))
        return(var1.grade > var2.grade) ? var1 : var2;
    return (var1 > var2) ? var1 : var2;
}
int main() {

    int i = 39;
    int j = 20;
    cout << "Max(i,j)=" << Max(i, j)<<endl;
    double f1 = 13.5;
    double f2 = 20.7;
    cout << "Max(f1,f2)=" << Max(f1, f2) << endl;
    string s1 = "Hello";
    string s2 = "World";
    cout << "Max(s1,s2)="<<Max(s1,s2) << endl;
    Student first_student;
    Student second_student;
    first_student.name = new char[30];
    second_student.name = new char[30];
    strcpy(first_student.name, "Popescu David");
    strcpy(second_student.name,"Gigel Petrovici");
    first_student.grade = 7;
    second_student.grade = 6;
    cout << "Max(student1,student2)=" << Max(first_student, second_student).grade << endl;
    return 0;
}

但我遇到了这些错误:

1>------ Build started: Project: OOP, Configuration: Debug Win32 ------
1>LastTODO.cpp
1>c:\users\dragos\source\repos\oop\oop\lasttodo.cpp(11): error C2228: left of '.grade' must have class/struct/union
1>c:\users\dragos\source\repos\oop\oop\lasttodo.cpp(11): note: type is 'T'
1>        with
1>        [
1>            T=int
1>        ]
1>c:\users\dragos\source\repos\oop\oop\lasttodo.cpp(22): note: see reference to function template instantiation 'T Max<int>(T,T)' being compiled
1>        with
1>        [
1>            T=int
1>        ]
1>c:\users\dragos\source\repos\oop\oop\lasttodo.cpp(11): error C2039: 'grade': is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>d:\visual studio\vc\tools\msvc\14.16.27023\include\xstring(4373): note: see declaration of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>c:\users\dragos\source\repos\oop\oop\lasttodo.cpp(28): note: see reference to function template instantiation 'T Max<std::string>(T,T)' being compiled
1>        with
1>        [
1>            T=std::string
1>        ]
1>c:\users\dragos\source\repos\oop\oop\lasttodo.cpp(12): error C2676: binary '>': 'T' does not define this operator or a conversion to a type acceptable to the predefined operator
1>        with
1>        [
1>            T=Student
1>        ]
1>c:\users\dragos\source\repos\oop\oop\lasttodo.cpp(38): note: see reference to function template instantiation 'T Max<Student>(T,T)' being compiled
1>        with
1>        [
1>            T=Student
1>        ]
1>Done building project "OOP.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

从 C++17 开始,您可以使用 if constexpr 来请求以下行

    return(var1.grade > var2.grade) ? var1 : var2;

仅当 TStudent 时编译:

template<typename  T>
T Max(T var1, T var2) {
    if constexpr ( std::is_same_v<T, Student> )
        return(var1.grade > var2.grade) ? var1 : var2;
    else
        return (var1 > var2) ? var1 : var2;
}

Demo


另一种可能性是将 Max 的主要版本定义为:

template<typename  T>
T Max(T var1, T var2) {
    return (var1 > var2) ? var1 : var2;
}

并为 Student 结构提供特化:

template<>
Student Max<Student>(Student var1, Student var2) {
    return(var1.grade > var2.grade) ? var1 : var2;
}

关于c++ - 我如何修改此函数,以便如果函数的参数是特定结构,它会返回具有更大整数的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58362851/

相关文章:

c++ - 使用库编译 C++ 项目

c++ - PIN - 获取正在检测的二进制文件的进程 ID?

C++使用子类从模板继承

c# - 在 WPF 中为整个应用程序设置皮肤的推荐方法是什么?

ios - 在 Swift 3 中将结构写入 outputStream

c - 初学者从结构数组中删除第一个元素 (C)

c++ - 用同名的 typedef 替换预处理器宏

c++ - 根据枚举成员的值特化模板

c++ - 解决循环依赖

c - 在 C 中使用 memcpy 将数组复制到结构,反之亦然