1

I have some code implemented in template variadic functions that uses modern c++17 features. Being templates, they are implemented in the .h files.

// .H FILE
template <typename... T>
inline constexpr void foo(const T& ...values){
    // Do stuff
}

Is there a way to create a compatibility layer that would allow users to access this functions from C?

  • 1
    I think you'll need to provide a subset of the functionality in the C API. When I've done similar things I've just picked the most important things first and made wrapper functions and wrapper types that only support a very specific subset of everything the real C++ functions could handle. – Ted Lyngmo Dec 21 '21 at 17:32
  • create dll/so and access these functions from there – Mayur Dec 21 '21 at 17:33
  • @Mayur How would the C declaration of that dll/so function look? – Ted Lyngmo Dec 21 '21 at 17:34
  • There are some [similar](https://stackoverflow.com/questions/2744181/how-to-call-c-function-from-c) [questions](https://stackoverflow.com/questions/40678385/calling-a-template-function-from-a-c-file). – 1201ProgramAlarm Dec 21 '21 at 17:38
  • Ok, I may try calling the template functions from some C code in a .C file. It seems like C has fairly nice support for variadic functions as well, so fingers crossed... – Marcos Alvarez Dec 21 '21 at 17:40
  • right, I misunderstood the Q, approach by @TedLyngmo seems a way to go – Mayur Dec 21 '21 at 17:41
  • 2
    You can declare C-compatible functions to call your template. Like `extern "C" void foo_int(int v) { foo(v); }`. – 1201ProgramAlarm Dec 21 '21 at 17:42
  • 1
    @MarcosAlvarez "*It seems like C has fairly nice support for variadic functions as well*" - yes, however C variadic functions and C++ variadic templates are VERY different beasts. A C++ variadic template can call a C variadic function, but I don't think the reverse is possible since the C function doesn't know what it is called with in order to populate the template parameters. – Remy Lebeau Dec 21 '21 at 18:05
  • A very odd API could demand that the C users actually use a part of the C++ API if this part is cumbersome to wrap up in nice C functions. They would have to write a C++ -> C wrapper function on their side instead which shouldn't be to hard if they get your help. – Ted Lyngmo Dec 22 '21 at 07:41
  • 1
    @TedLyngmo I agree. However, that is a business decision that I have no control over. The problem is solved though. The wrapper may have a tiny bit of overhead, but it is not a deal breaker. – Marcos Alvarez Dec 22 '21 at 14:07

1 Answers1

0

The way I actually solved may not be valid to all particular cases!! I found that trying to pass arguments directly to a c++ variadic function was not possible (to the best of my knowledge). Instead, a void vector would be passed, and the results will not be the ones expected. In this case, stringifying the c input, and passing it to the c++ function worked just fine.

#ifdef __cplusplus
extern "C" {
#endif
    void cfoo(const char * fmt, ...)
    {
        va_list args
        va_start(args, fmt);
        char str[1024];
        vsprintf(str, fmt, args);
        cpp::foo(str); // My c++ function
        va_end(args);
    }
#ifdef __cplusplus
}
#endif