Created
June 22, 2020 08:54
-
-
Save AndrewLipscomb/24f61a5e0c015c523eafc2daf640de6f to your computer and use it in GitHub Desktop.
3rd Attempt at C++ q
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "./templates_a.h" | |
int main() | |
{ | |
LocalTypeA bbb; | |
bbb.internal_ = new LocalTypeB(); | |
do_foo<LocalTypeA>(LocalTypeA()); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "./templates_b.h" | |
int main() | |
{ | |
do_foo<double>(0.0); | |
do_foo<LocalTypeB>(LocalTypeB()); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "./templates_top.h" | |
#include "./templates_b.h" | |
class LocalTypeA { | |
public: | |
LocalTypeB* internal_; | |
}; | |
template<typename T> | |
std::enable_if_t<std::is_same<T, LocalTypeA>::value, int> do_foo(T bb) | |
{ | |
// Some detailed business logic | |
// specific to LocalTypeA and LocalTypeB that ideally isn't in a header | |
return do_foo<LocalTypeB>(*(bb.internal_)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef HEADER_B | |
#define HEADER_B | |
#include <type_traits> | |
#include "templates_top.h" | |
class LocalTypeB { | |
public: | |
bool the_val = false; | |
}; | |
template<typename T> | |
std::enable_if_t<std::is_same<T, LocalTypeB>::value, int> do_foo(T bb) | |
{ | |
// Some detailed business logic here | |
// specific to LocalTypeB that ideally isn't in a header | |
// Also - we need to invoke generic impl do_foo | |
return do_foo<bool>(bb.the_val); | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TOPHEADER | |
#define TOPHEADER | |
#include <type_traits> | |
template<typename T> | |
std::enable_if_t<std::is_fundamental<T>::value, int> do_foo(T bb) | |
{ | |
// Some generic business logic for all fundamental types here | |
return 0; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment