Skip to content

Instantly share code, notes, and snippets.

@AndrewLipscomb
Created June 22, 2020 08:54
Show Gist options
  • Save AndrewLipscomb/24f61a5e0c015c523eafc2daf640de6f to your computer and use it in GitHub Desktop.
Save AndrewLipscomb/24f61a5e0c015c523eafc2daf640de6f to your computer and use it in GitHub Desktop.
3rd Attempt at C++ q
#include "./templates_a.h"
int main()
{
LocalTypeA bbb;
bbb.internal_ = new LocalTypeB();
do_foo<LocalTypeA>(LocalTypeA());
return 0;
}
#include "./templates_b.h"
int main()
{
do_foo<double>(0.0);
do_foo<LocalTypeB>(LocalTypeB());
return 0;
}
#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_));
}
#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
#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