Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // selector.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <iostream>
- struct S1 {
- void some_func() {
- std::cout << __func__ << std::endl;
- }
- };
- struct S2 {
- void some_func() {
- std::cout << __func__ << std::endl;
- }
- };
- struct S3 {
- void some_another_func() const {
- std::cout << __func__ << std::endl;
- }
- };
- template <typename T>
- struct is_S3 {
- static const bool value = false;
- };
- template <>
- struct is_S3<S3> {
- static const bool value = true;
- };
- template <bool BOOL>
- struct struct_selector {
- template<typename T>
- static void select(T & t) {
- // fake
- }
- };
- template <>
- struct struct_selector<true> {
- template<typename T>
- static void select(T & t) {
- t.some_another_func();
- }
- };
- template <>
- struct struct_selector<false> {
- template<typename T>
- static void select(T & t) {
- // fake
- t.some_func();
- }
- };
- struct lsf : S3 {
- void some_func() {
- S3::some_another_func();
- }
- void some_another_func() {
- std::cout << "caveat" << std::endl;
- }
- };
- template <typename T>
- void select(T & t) {
- struct_selector<is_S3<T>::value>::select(t);
- }
- int main(int argc, char** argv) {
- S1 s1;
- S2 s2;
- S3 s3;
- lsf ls;
- select(s1);
- select(s2);
- select(s3);
- select(ls);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement