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 6353395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:24:17+00:00 2026-05-24T22:24:17+00:00

I want to write several functions that are only different in the types of

  • 0

I want to write several functions that are only different in the types of arguments. I know C++ has template to handle this problem well (not very well yet though, few compilers support export keyword and this keyword is queried for efficiency). For easy example, I want:

template <typename T>
T add(T a, T b){
    return a+b;
}

However, in pure C (sometimes I have to choose pure C, as some platform doesn’t have the C++ compiler), there have to be different function names for different versions, as

double addDouble(double a, double b){
    return a+b;
}

int addInt(int a, int b){
    return a+b;
}

Hmmm, when there are two versions, it seems OK that I can do the copy-and-paste work in a source file; However, in practice there would be many lines instead of just a return statement in a function and would be more versions. So, my question is, how to implement a series of functions in different type versions elegantly?

So far I have tried some solutions as below, but I think they are far from good. I need your suggestions, thank you!

Solution 1:

#define mytype int
mytype addInt(mytype a, mytype b){
    return a+b;
}
#undef mytype

#define mytype float
mytype addFloat(mytype a, mytype b){
    return a+b;
}
#undef mytype

Shortcoming of Solution 1: duplicated contents are too many, and if I want to modify the function, I have to modify all of versions.

Solution 2:

func.h

#ifndef FUNC_H
#define FUNC_H

#define add(a, b, typename) functionAdd##typename(a,b)

/* function declarations */
#define declared(typename) \
typename functionAdd##typename(typename, typename)

declared(int);
declared(float);

#endif

func.c

#include "func.h"

/* function code */
#define functionAdd(a, b, typename) \
typename functionAdd##typename(typename a, typename b){ \
    return a+b; \
}

/* function bodies (definitions) */
functionAdd(a, b, int)
functionAdd(a, b, float)

main.c

#include <stdio.h>
#include "func.h"

int main()
{
    int x1 = add(1, 2, int);
    float x2 = add(3.0, 4.0, float);
    printf("%d %f\n", x1, x2);  
    return 0;
}

Shortcoming of Solution 2: Because the function is written in define, it’s difficult to debug. Besides, the \ notation is annoying. Though, it’s convenient to add a new version, just inserting declared(double) into func.h and functionAdd(a, b, double) into func.c will achieve this aim.

  • 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-05-24T22:24:19+00:00Added an answer on May 24, 2026 at 10:24 pm

    In many (if not most) cases the best way to simulate C++ templates in C would be Solution 3: parametrized header file and parametrized implementation file. In your case it would work as follows

    1. Create a meta-header file, which we’ll name add.dec, that looks as follows

      TYPE_ CONCAT(add, SUFFIX_)(TYPE_ a, TYPE_ b);
      TYPE_ CONCAT(sub, SUFFIX_)(TYPE_ a, TYPE_ b);
      
    2. Create a meta-implementation file, which we’ll name add.def, that looks as follows

      TYPE_ CONCAT(add, SUFFIX_)(TYPE_ a, TYPE_ b){
        return a + b;
      }
      
      TYPE_ CONCAT(sub, SUFFIX_)(TYPE_ a, TYPE_ b){
        return a - b;
      }
      

    These two files are parametrized by two macros: TYPE_ and SUFFIX_, while CONCAT is a traditional implementation of macro concatenation

    #define CONCAT_(a, b) a##b
    #define CONCAT(a, b) CONCAT_(a, b)
    

    Now, imagine you want to instantiate your “template” functions for types int and double. In a “real” header file add.h you simply do

    #define TYPE_ int
    #define SUFFIX_ Int
    #include "add.dec"
    #undef TYPE_
    #undef SUFFIX_
    
    #define TYPE_ double
    #define SUFFIX_ Double
    #include "add.dec"
    #undef TYPE_
    #undef SUFFIX_
    

    and in a “real” implementation file add.c you do

    #define TYPE_ int
    #define SUFFIX_ Int
    #include "add.def"
    #undef TYPE_
    #undef SUFFIX_
    
    #define TYPE_ double
    #define SUFFIX_ Double
    #include "add.def"
    #undef TYPE_
    #undef SUFFIX_
    

    That’s it. By doing this you instantiated (declared and defined) addInt, addDouble, subInt and subDouble.

    Of course, you can parametrize the declarations much more. You can add a DECLSPEC_ parameter to be able to declare your sunctions as static, if necessary. You can specify different types for parameters and return values (say, ARG_TYPE_ and RET_TYPE_). You can parametrize lots of other things. Basically, there’s no limit to what you can parametrize. With some fairly easy macro techniques you can even parametrize the number of parameters your functions expect.

    This is actually similar to your Solution 1 and Solution 2 combined. This basically takes the best from both of your approaches. And I’d say that this is the most faithful attempt to simulate the behavior of C++ template instantiation.

    Note that each function’s body is explicitly typed only once (as opposed to multiple explicit copies in your Solution 1). The actual function bodies are also easily editable, since there’s no need to worry about those pesky \ at the end of each line (as is the case in your Solution 2).

    This approach has another interesting benefit: the code in add.def will remain “debuggable”, i.e. an ordinary interactive debugger will typically be able to step into these implementations (which is impossible in your Solution 2).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several embedded linux systems that I want to write a 'Who's Online?'
I have several lists in Matlab that I want to write to the same
I want to write a function in Python that returns different fixed values based
I'm trying to write a simple app that has several internal pages. From #page2
I'm struggling with this for several days now. I want to create a functions
I have an object with several text strings as members. I want to write
I want to write a command that specifies the word under the cursor in
I want to write a function that takes an array of letters as an
I'm writing a Chrome extension, and want to write one JS file, that provides
I want to create a multilne variable that will be split over several-lines, indenting

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.