Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. // Impl.h
  2. #pragma once
  3.  
  4. struct A {
  5. struct {
  6. int value;
  7. } valueA;
  8. };
  9.  
  10. struct B {
  11. struct {
  12. int value;
  13. } valueB;
  14. };
  15.  
  16. template<typename T>
  17. int GetValue(T const &value);
  18.  
  19. // Impl.cpp
  20. #include "Impl.h"
  21. #include <type_traits>
  22.  
  23. using std::enable_if_t;
  24. using std::remove_reference_t;
  25.  
  26. template<typename T, typename U, U(T::*Value)>
  27. static inline int GetValueImpl(T const &value) {
  28. return (value.*Value).value;
  29. }
  30.  
  31. template<typename T>
  32. enable_if_t<T::valueA, int> GetValue(T const &value) {
  33. static constexpr auto T::*const Value = &T::valueA;
  34. typedef remove_reference_t<decltype(value.*Value)> ValueType;
  35. return GetValueImpl<T, ValueType, Value>(value);
  36. }
  37.  
  38. template<typename T>
  39. enable_if_t<T::valueB, int> GetValue(T const &value) {
  40. static constexpr auto T::*const Value = &T::valueB;
  41. typedef remove_reference_t<decltype(value.*Value)> ValueType;
  42. return GetValueImpl<T, ValueType, Value>(value);
  43. }
  44.  
  45. template<> int GetValue(A const &); // C2912 here
  46. template<> int GetValue(B const &); // C2912 here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement