#include <iostream>
#include <string>
#include <type_traits>
using namespace std;
struct boy_tag
{
};
struct girl_tag{
};
struct boy
{
void eat() const
{
cout << "eat two bowls";
}
};
struct girl
{
void eat() const
{
cout << "eat one bowl"<<endl;
}
};
template <typename T>
void eat(T student, girl_tag)
{
cout << "---"
<< "girl"
<< "---" << endl;
student.eat();
}
template <typename T>
void eat(T student, boy_tag)
{
cout << "---"
<< "boy"
<< "---" << endl;
student.eat();
}
template <typename T>
void eat(T student)
{
bool flag = is_same<decltype(student), boy>::value;
if(flag){
eat(student,boy_tag{});
}else{
eat(student,girl_tag{});
}
}
int main(void)
{
girl g;
boy b;
// eat(g);
eat(b);
return 0;
}
C++ 自定义标签和使用标签分发
最新推荐文章于 2024-06-02 16:53:56 发布