Advertisement
Guest User

Untitled

a guest
May 16th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. // test_template_variadic_inheritance.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include <string>
  7. #include <iostream>
  8. #include <type_traits>
  9.  
  10. class Base {
  11.     std::string m_Value;
  12. public:
  13.     Base(std::string some_value)
  14.         : m_Value{ some_value }  {
  15.     }
  16.  
  17.     void PrintValue() {
  18.         std::cout << m_Value << std::endl;
  19.     }
  20. };
  21.  
  22. class SimpleBase {
  23. public:
  24.     SimpleBase() = default;
  25.  
  26.     void PrintValue() {
  27.         std::cout << "simple base value " << std::endl;
  28.     }
  29. };
  30.  
  31. template<typename T>
  32. class Derived : public T {
  33. public:
  34.     template< typename ... T_CTOR_ARGS >
  35.     Derived(T_CTOR_ARGS ... args)
  36.         : T ( std::forward<T_CTOR_ARGS>( args )... ) {
  37.  
  38.     }
  39.  
  40.     void SomeFunctionality() {
  41.         T::PrintValue();
  42.     }
  43. };
  44.  
  45. template<typename T, typename ... ARGS>
  46. Derived<T> create_derived(ARGS... args) {
  47.     return Derived<T>{ std::forward<ARGS>(args)... };
  48. }
  49.  
  50. int main()
  51. {
  52.     Derived<Base> base = create_derived<Base>(std::string{ "test" });
  53.     Derived<SimpleBase> simple_base = create_derived<SimpleBase>( );
  54.  
  55.     base.PrintValue();
  56.     simple_base.PrintValue();
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement