Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::sort & comp - calling convention?

Windows targeted project, Visual Studio 2012

I'm trying to use std::sort to sort an array of struct pointers. The sorting is meant to be done from a GUID contained within the struct, and so I want to define a custom compare function for my sort call.

std::sort(
    std::begin(pUnits),
    std::end(pUnits),
    MyCustomSortFunctionHere
);

Now my question is, what's the expected calling convention for the provided compare function? Question rather could be, does the calling convention even make a difference here?

Reason I need to know is, my project settings play with the default calling conventions and so if I declare my compare function without explicitly declaring the calling convention, I'm wondering if it would break. Can't seem to find any information about this anywhere.

Thanks.

like image 397
Verv Avatar asked Nov 21 '25 01:11

Verv


1 Answers

My reasoning is that MyCustomSortFunctionHere function (or class methods?) is called with the calling convention you ask it to: with

extern "C" bool MyCustomSortFunctionHere(Unit const*, Unit const*);

it would be called with "C" calling convention. Since std::sort() is a template function, it is actually compiled together with your code in its same translation unit, including the part where your comparison function is called, and the call to MyCustomSortFunctionHere follows the rules you have specified in that translation unit.

If you are defining the comparison function in the same translation unit, you should get away with not specifying any convention. If the function is instead defined in a different translation unit, you would need to express to the compiler which is the right calling convention.

like image 55
Hamlet Avatar answered Nov 22 '25 13:11

Hamlet