Last active
          August 29, 2015 14:07 
        
      - 
      
- 
        Save wallstop/b04a93b095b443b0f3ef to your computer and use it in GitHub Desktop. 
    Simple Create Pattern
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | #pragma once | |
| #include <utility> | |
| template <typename T, typename ... Args> | |
| T create(Args... args) | |
| { | |
| return std::move(T(args...)); | |
| } | |
| template <> | |
| int create<int>() // Only override default constructor for int | |
| { | |
| return 9001; | |
| } | |
| // Override templates as need be | 
  
    
      This file contains hidden or 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 "Create.h" | |
| int main() | |
| { | |
| int i = create<int>(); // Should be 9001 | |
| int b = create<int>(1); | |
| int* c = new int(create<int>(500)); | |
| return 0; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment