Вчера я заметил, что часть моего кода можно подвергнуть рефакторингу таким образом.
Большая часть стандартного кода уйдет. Единственным недостатком будет то, что вместо копирования, единицы и нуля (5-6 таких) мне нужно будет переключиться на отправку тегов, например. s.process(Copy{}, 5)
Что это за идиома или шаблон дизайна?
Для меня это немного похоже на «отправка сообщения» определенному классу в структуре.
Также немного похоже на «декоратор».
Ссылка Godbolt:
https://gcc.godbolt.org/z/oGnvT6j9e
#include // std::forward
struct Copy{};
struct One{};
struct Zero{};
struct S{
template
constexpr static auto process(Tag, Ts const & ...){
}
constexpr auto process(Copy, int x) const{
// not a static for demo reasons
return x;
}
};
struct S2{
S s;
template
constexpr auto process(Tag tag, Ts && ...ts){
return s.process(tag, std::forward(ts)...);
}
template
constexpr auto process(Tag tag, Ts && ...ts) const{
return s.process(tag, std::forward(ts)...);
}
constexpr auto process(One){
// not a const for demo reasons
return 1;
}
constexpr static auto process(Zero){
return 0;
}
};
struct S3{
S2 s;
template
constexpr auto process(Tag tag, Ts && ...ts){
return s.process(tag, std::forward(ts)...);
}
template
constexpr auto process(Tag tag, Ts && ...ts) const{
return s.process(tag, std::forward(ts)...);
}
};
int main(){
S3 s;
return s.process(Copy{}, 5) + s.process(One{}) + s.process(Zero{});
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... rn-is-this