Thread: Can I use templates to add code in place?

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    108

    Can I use templates to add code in place?

    Is there a way to use templates to place code in place. What I want to do is mimic the behavior of "#define" but using C++ templates. So I want to do something like the following:

    Code:
    #define exit_the_prog                                            \
     printf("Error: There was an error with the program");  \
     exit(2);
    Is there a way to do that?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,075
    For something that simple, just use an inline function.
    Code:
    static inline void exit_the_prog() {
        printf("Error: There was an error with the program");
        exit(2);
    }
    Templates are for when you want to parametrise something generic in some way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates and Templates objects
    By Dave11 in forum C++ Programming
    Replies: 6
    Last Post: 07-05-2014, 06:27 AM
  2. Am I in the right place?
    By RVC in forum C Programming
    Replies: 15
    Last Post: 03-08-2011, 09:37 AM
  3. New to Forms/MFC. Unsure where to place the code.
    By Swerve in forum Windows Programming
    Replies: 1
    Last Post: 02-02-2009, 01:15 AM
  4. is c# going take c++'s place?
    By onurak in forum C++ Programming
    Replies: 10
    Last Post: 07-08-2002, 11:59 PM

Tags for this Thread