Pointers to functions and methods in std :: set

As described in another post , it is not possible to match 2 pointers to member functions with "<" (less) . At least this leads to undefined behavior.

I just managed to compile this code with both Visual C ++ and GCC:

template <class Receiver, class Param = void*, class Return = void> class EventReceiver : public IFunction<> { protected: std::set< Return(Receiver::*)(Param) > test; std::set< Return(*)(Param) > test2; ... 

AFAIK, in order to do std :: map or std :: set of anything, it should be possible to compare the set values โ€‹โ€‹with "<". Does this mean that the above containers or actual compilers have a working implementation of comparing method pointers in this way?

+4
source share
2 answers

Well, itโ€™s actually misleading that the compiled example code. In truth, the kits are unusable . Each attempt to insert data into them creates an expected error.

What works the "dark side" of the C ++ template. They do not exist until you use them (and therefore will not create compiler errors until you do this).

Check it:

 #include <set> class X {}; int main() { typedef void(X::*FuncPtr)(); std::set< FuncPtr > set; std::less< FuncPtr > less; FuncPtr f1; FuncPtr f2; //set.insert(f1); // both of these lines //less(f1,f2); // produce an error }; 

Removing comments on either of the last two lines results in an error:

invalid operands of types void (X :: * const) () and 'void (X :: * const) () in the binary' operator <

Compile it right here .

0
source

std::set<T> uses std::less<T> instead of directly using the less operator. If T is a pointer type, std::less<T> guaranteed to be in full order, even if the statement is less.

Edit: section 20.3.3 of the C ++ 98 standard says "pointer types", which (I think) include function pointers, but not member pointers.

0
source

Source: https://habr.com/ru/post/1395473/


All Articles