Advertisement
Guest User

compile-time constant factory with templated parameter set

a guest
May 30th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. /** requires gcc5.0+ and C++14
  2.  * g++ -std=c++14 -o test test.cpp
  3.  */
  4. #include <iostream>
  5.  
  6. /**
  7.  * Struct with compiler-constant parameters
  8.  */
  9. struct FFT_Inplace {
  10.   static constexpr bool IsInplace = true;
  11.   //..
  12. };
  13.  
  14. /**
  15.  * ... same, but different values ...
  16.  */
  17. struct FFT_Outplace {
  18.   static constexpr bool IsInplace = false;
  19. };
  20.  
  21. /**
  22.  * Our implementation for an FFT Plan+Execution.
  23.  */
  24. template<typename TFFT>
  25. struct PlanImpl {
  26.   static constexpr bool IsInplace = TFFT::IsInplace;
  27.   void operator()() {
  28.     std::cout << "IsInplace = " << IsInplace << std::endl;
  29.   }
  30. };
  31.  
  32. /**
  33.  * Compiler-constant factory
  34.  * TFFT is struct with compiler-constant parameters
  35.  */
  36. template<
  37.   typename TFFT,
  38.   template<typename> typename TPlan
  39.   >
  40. constexpr
  41. auto makePlan() {
  42.   return TPlan<TFFT>();
  43. }
  44.  
  45. /**
  46.  * Let's play
  47.  */
  48. int main(void)
  49. {
  50.   auto inplace = makePlan<FFT_Inplace,PlanImpl>();
  51.   inplace();
  52.   auto outplace = makePlan<FFT_Outplace,PlanImpl>();
  53.   outplace();
  54.   return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement