Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8377811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:46:03+00:00 2026-06-09T15:46:03+00:00

I was just messing about, when I discovered this pre-processor dependent way of creating

  • 0

I was just messing about, when I discovered this pre-processor dependent way of creating a template class:

#include <iostream>
#include <typeinfo>

// Is this a valid template class?
#define TEMPLATE_CLASS(T)\
class TemplateClass_ ## T\
{\
private:\
    T value;\
public:\
    void print_type()\
    {\
        std::cout << typeid(T).name() << std::endl;\
    }\
}

class Sample {};

TEMPLATE_CLASS(int) obj1;
TEMPLATE_CLASS(char) obj2;
TEMPLATE_CLASS(Sample) obj3;

int main(int argc, char* argv[])
{
    obj1.print_type();
    obj2.print_type();
    obj3.print_type();
}

I compiled this simply with:

g++ src.cpp -o main.exe

The output:

i
c
6Sample

Now, as you can see, it pretty much works the same way as template classes. Except for the obvious fact than the objects can only be declared globally, since it actually defines a new class inline when you use TEMPLATE_CLASS(T), and new classes cannot be defined within a function. A solution to this can be:

TEMPLATE_CLASS(float);

int main() { TemplateClass_float obj; }

Anyway, this gave me a lot to think about. Firstly, can this even be called a valid generic class? And so, can it possibly be used instead of the standard template feature? Obviously, using the standard template feature is a lot more convenient, but what I mean to say is, will this work just as well? Lastly, does the template feature defined by the C++ standard internally do something similar to what I’m doing with the pre-processor? If not, what are the differences between this implementation and C++’s standard template feature?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:46:05+00:00Added an answer on June 9, 2026 at 3:46 pm

    It does the same thing as a template only in the simplest cases. Try doing this with macros:

    template <class Ty> class C {};
    template <class Ty> class C<Ty*> {};
    template <class Ty> class C<std::vector<Ty> > {};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im just messing around with Ruby on Rails and HTML. This question isn't about
I'm just messing around learning about JavaScript and I wanted to change the color
In an effort to learn about synchronization via Java, I'm just messing around with
I just started learning about servers, and I am messing around with Google's App
just started messing with the android sdk and need to think about optimizing my
I am trying to get inheritance in JS. I just discovered a neat way
Just messing about with F# and I was trying to create a basic Lagrange
I'm just messing around trying to learn a little bit about parallel computing. If
This is doing my head right in! I am messing about with JRuby trying
I'm just messing about trying to figure out cakephp which looks like it's going

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.