1

I know that templates are not defined in C. However in my case, i have an API written in C++, which is used by an application written in C. I wish to add a template function in the API. The function is defined as follows in abc.cpp:

template<typename T> T function_name(T param1){
  ...
  ...
  return val;
}

the declaration in abc.hpp as follows:

template<typename T> T function_name(T);

and this function is called from xyz.c as:

int a ,b = 5;
a = function_name(b);

However, it shows the following error in both abc.cpp and abc.hpp:

 error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
 template<typename T>

Even using extern "C" doesn't help(leads to error: template with C linkage). My doubt is, is it even possible to call this template function in such a way? If yes, how can this be achieved? Thank you.

7
  • 4
    The "error: template with C linkage" error is the hint you should be taking; it is not intended that it can be done with C calling C++ directly. You'd have to know the mangled C++ name for an instantiation of the template function, and then the C code would call that. That ties you to a particular C++ compiler (possibly even a specific version of the compiler). The correct way to do it is extern "C" int template_int_function_name(int param1) { return function_name(param1); } and then the C calls template_int_function_name(). This gives you a C-callable wrapper for the template function. Commented Nov 18, 2016 at 13:37
  • The application may suddenly turn out to be written in C++ ;-) (if you decide to compile it with a C++ compiler). Chances are that you need minor adaptions, in particular if the code follows unwind's advice to not cast the return value of malloc(), but the edits are usually fairly minor. Commented Nov 18, 2016 at 13:39
  • @JonathanLeffler Re "call template function directly through mangled name": The instantiation may not even exist (and lead to a link-time error) if no C++ code, i.e. here no code inside the library, calls the function with an int argument. If the function is a recent addition to a library that is rather likely. Commented Nov 18, 2016 at 13:41
  • @PeterA.Schneider: Yes; that's another possible defect with trying to call the mangled name. Unlikely, but possible. And just another nail in the coffin of a thoroughly dead idea — it is absolutely not the correct way to go. Commented Nov 18, 2016 at 13:46
  • 1
    Yes, you would need wrappers for every type that needs to be used by the C code. It would be sensible not to use the template functions from C. Rewriting those functions in C++ may be better, but idiomatic modern C++ is very different from C. Commented Nov 19, 2016 at 14:08

3 Answers 3

9

No, of course not, template functions don't exist in C and you can't parse a template declaration with a C compiler.

You're going to have to add a C wrapper on the C++ side for the int instance of the template, and call that (e.g. something like function_int()).

Sign up to request clarification or add additional context in comments.

Comments

1

Since you mix C and C++ already (and therefore must have taken care of the possible issues), another solution is to continue developing the application in C++. That way new code can use all the nice features of the C++ library, like templates ;-), overloaded functions etc., and has access to C++'s standard library which can greatly enhance productivity.

I don't see principal obstacles to using C++ for new source files in the application; whether it's easily possible and worthwhile to switch existing C files to C++ upon edits is another question.

Comments

0

Since C does not support templates, you can consider to use Type-Generic Functions - a macro-based method of defining generic expressions.

If this is not a case, I suggest to stay with C++ compiler.

1 Comment

If you mean _Generic, it is not a macro-based solution but is a full operator in the language — it is described in the standard in section §6.5.1.1 Generic selection, which is part of section §6.5.1 Primary expressions. Section 6.10 describes the preprocessor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.