Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent overloading for extern "C" functions?

I'm writing a c++ library that exposes some functions which are used only by C# code. However, as I accidently mistyped the paramter, I found that this code can be succesfully compiled and linked even without any warning as long as I don't use the (not mistyped version) function in the cpp file.

struct Dummy { int a; double b; };
extern "C" void SetArray(Dummy* x, int cnt);
void SetArray(Dummy x, int cnt)
{
    // a TODO placeholder.
}

How can I let compiler throw an error or a warning for this case? The compiler option -Wall is set but there's still no warning. Using tdmgcc 5.1.0.

like image 538
Apliex-Ddr Avatar asked Aug 03 '18 10:08

Apliex-Ddr


People also ask

Can we prevent overloading If yes code it and explain it?

So, if he knows this code (theoretically) and there is some inherent danger in overloading, then he should know this already because he knows the code. That said, overloading cannot be stopped as others have already described.

Do C++ features are allowed in extern C block?

Not any C-header can be made compatible with C++ by merely wrapping in extern "C".

Why does C does not support overloading?

This feature is present in most of the Object Oriented Languages such as C++ and Java. But C doesn't support this feature not because of OOP, but rather because the compiler doesn't support it (except you can use _Generic). However, one can achieve the similar functionality in C indirectly.


1 Answers

You can make some assertion that will fail if function is overloaded:

static_assert(::std::is_same_v<void (Dummy *, int), decltype(SetArray)>);

error: decltype cannot resolve address of overloaded function

like image 129
user7860670 Avatar answered Oct 12 '22 12:10

user7860670