Skip to content

Commit 088caa6

Browse files
committed
refactor: use "if constexpr" in std::vector's Serialize()
This gets rid of unnecessarily creating a temporary object T() to call the right function.
1 parent 0fafaca commit 088caa6

File tree

1 file changed

+16
-31
lines changed

1 file changed

+16
-31
lines changed

src/serialize.h

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,6 @@ template<typename Stream, unsigned int N, typename T> inline void Unserialize(St
649649
* vector
650650
* vectors of unsigned char are a special case and are intended to be serialized as a single opaque blob.
651651
*/
652-
template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&);
653-
template<typename Stream, typename T, typename A> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&);
654-
template<typename Stream, typename T, typename A, typename V> void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&);
655652
template<typename Stream, typename T, typename A> inline void Serialize(Stream& os, const std::vector<T, A>& v);
656653
template<typename Stream, typename T, typename A> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&);
657654
template<typename Stream, typename T, typename A, typename V> void Unserialize_impl(Stream& is, std::vector<T, A>& v, const V&);
@@ -783,38 +780,26 @@ void Unserialize(Stream& is, prevector<N, T>& v)
783780
/**
784781
* vector
785782
*/
786-
template<typename Stream, typename T, typename A>
787-
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const unsigned char&)
788-
{
789-
WriteCompactSize(os, v.size());
790-
if (!v.empty())
791-
os.write(MakeByteSpan(v));
792-
}
793-
794-
template<typename Stream, typename T, typename A>
795-
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const bool&)
783+
template <typename Stream, typename T, typename A>
784+
void Serialize(Stream& os, const std::vector<T, A>& v)
796785
{
797-
// A special case for std::vector<bool>, as dereferencing
798-
// std::vector<bool>::const_iterator does not result in a const bool&
799-
// due to std::vector's special casing for bool arguments.
800-
WriteCompactSize(os, v.size());
801-
for (bool elem : v) {
802-
::Serialize(os, elem);
786+
if constexpr (std::is_same_v<T, unsigned char>) {
787+
WriteCompactSize(os, v.size());
788+
if (!v.empty())
789+
os.write(MakeByteSpan(v));
790+
} else if constexpr (std::is_same_v<T, bool>) {
791+
// A special case for std::vector<bool>, as dereferencing
792+
// std::vector<bool>::const_iterator does not result in a const bool&
793+
// due to std::vector's special casing for bool arguments.
794+
WriteCompactSize(os, v.size());
795+
for (bool elem : v) {
796+
::Serialize(os, elem);
797+
}
798+
} else {
799+
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
803800
}
804801
}
805802

806-
template<typename Stream, typename T, typename A, typename V>
807-
void Serialize_impl(Stream& os, const std::vector<T, A>& v, const V&)
808-
{
809-
Serialize(os, Using<VectorFormatter<DefaultFormatter>>(v));
810-
}
811-
812-
template<typename Stream, typename T, typename A>
813-
inline void Serialize(Stream& os, const std::vector<T, A>& v)
814-
{
815-
Serialize_impl(os, v, T());
816-
}
817-
818803

819804
template<typename Stream, typename T, typename A>
820805
void Unserialize_impl(Stream& is, std::vector<T, A>& v, const unsigned char&)

0 commit comments

Comments
 (0)