weidai11 / cryptopp

free C++ class library of cryptographic schemes
https://cryptopp.com
Other
4.83k stars 1.49k forks source link

Using emplace_* instead of push_* #1215

Open irwir opened 1 year ago

irwir commented 1 year ago

emplace_* methods are more efficient when adding new objects, but might require C++11. Does it make sense to add code for this? Tests run successfully for the branch https://github.com/irwir/cryptopp/tree/use_emplace

noloader commented 1 year ago

Test the code with:

CXXFLAGS="-std=c++03" make -j 4
irwir commented 1 year ago

These flags are for GCC? I do not have it installed and almost always use Visual Studio, where currently the lowest possible seems to be /std:c++14 C++03 standards do not include emplace; it might be implemented but this could not be relied on.

irwir commented 1 year ago

This replacement could be done with a simple macro possibly named as CRYPTOPP_EMPLACE_BACK. The definition may depend on CRYPTOPP_CXX11, or it could be a special CRYPTOPP_EMPLACE flag if necessary.

irwir commented 1 year ago

// emplace_front and emplace_back have been defined in C++11

ifndef CRYPTOPP_CXX11_EMPLACE

define CRYPTOPP_CXX11_EMPLACE 1

endif

The idea is to add this definition into config_cxx.h at the end of CRYPTOPP_CXX11 block, and after the CRYPTOPP_CXX11 block add CRYPTOPP_EMPLACE_BACK macro that expands to emplace_back or push_back method calls.

Is this good enough for a PR?