from key to function ptr
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
#include <unordered_map>
#include <map>
#include <functional>
#include <vector>
using namespace std;
enum A {
RGB=3, YUV=2, GRAY=4
};
template<A t, A tt>
void f() {
cout << "default" << endl;
cout << t << endl;
cout << tt << endl;
}
template<>
void f<A::RGB, A::YUV>() {
cout << "spec rgb -> yuv" << endl;
}
template<>
void f<A::YUV, A::RGB>() {
cout << "spec yuv -> rgb" << endl;
}
template<>
void f<A::YUV, A::GRAY>() {
cout << "spec yuv -> gray" << endl;
}
int main()
{
f<YUV,RGB>(); // yuv -> rgb
std::vector<std::pair<std::pair<A,A>, std::function<void()>> > vec;
vec.push_back(make_pair(make_pair(A::RGB, A::YUV), f<A::RGB,A::YUV>));
vec.push_back(make_pair(make_pair(A::YUV, A::GRAY), f<A::YUV,A::GRAY>));
vec.push_back(make_pair(make_pair(A::YUV, A::RGB), f<A::YUV,A::RGB>));
auto functor = vec[0].second;
functor(); // rgb->yuv
for (auto iter = vec.begin(); iter != vec.end(); iter++) {
if (iter->first == make_pair(A::YUV,A::GRAY)) {
(iter->second)(); // yuv -> gray
break;
}
}
cout << "split" << endl;
std::map<std::pair<A,A>, void(*)()> mmap;
mmap.emplace(make_pair(RGB, YUV), f<RGB,YUV>);
mmap.emplace(make_pair(YUV, GRAY), f<YUV,GRAY>);
mmap[make_pair(YUV,GRAY)]();
return 0;
}
本文来自博客园,作者:ijpq,转载请注明原文链接:https://www.cnblogs.com/ijpq/p/17096021.html