commit 7f9b957a61a6f2226d662ccefa6789804161f309
parent ab41492b069ccc7db90d91742d7cf97b0c4789fb
Author: Michael Savage <mikejsavage@gmail.com>
Date: Sun, 29 Oct 2017 22:48:01 +0200
Pass variadic args by const reference
Diffstat:
3 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/README.md b/README.md
@@ -152,7 +152,7 @@ std::string:
```cpp
template< typename... Rest >
-std::string ggformat_to_string( const char * fmt, Rest... rest ) {
+std::string ggformat_to_string( const char * fmt, const Rest & ... rest ) {
size_t space_required = ggformat( nullptr, 0, fmt, rest... );
if( space_required + 1 < space_required )
diff --git a/ggformat.h b/ggformat.h
@@ -24,13 +24,13 @@
*/
template< typename... Rest >
-size_t ggformat( char * buf, size_t len, const char * fmt, Rest... rest );
+size_t ggformat( char * buf, size_t len, const char * fmt, const Rest & ... rest );
template< typename... Rest >
-bool ggprint_to_file( FILE * file, const char * fmt, Rest... rest );
+bool ggprint_to_file( FILE * file, const char * fmt, const Rest & ... rest );
template< typename... Rest >
-bool ggprint( const char * fmt, Rest... rest );
+bool ggprint( const char * fmt, const Rest & ... rest );
/*
* structures and functions used for formatting specific data types
@@ -124,7 +124,7 @@ void ggformat_literals( FormatBuffer * fb, const char * literals, size_t len );
GGFORMAT_DISABLE_OPTIMISATIONS();
template< typename T, typename... Rest >
-void ggformat_impl( FormatBuffer * fb, const char * fmt, const T & first, Rest... rest ) {
+void ggformat_impl( FormatBuffer * fb, const char * fmt, const T & first, const Rest & ... rest ) {
size_t start, one_past_end;
bool has_fmt = ggformat_find( fmt, &start, &one_past_end );
GGFORMAT_ASSERT( has_fmt );
@@ -138,14 +138,14 @@ void ggformat_impl( FormatBuffer * fb, const char * fmt, const T & first, Rest..
}
template< typename... Rest >
-size_t ggformat( char * buf, size_t len, const char * fmt, Rest... rest ) {
+size_t ggformat( char * buf, size_t len, const char * fmt, const Rest & ... rest ) {
FormatBuffer fb( buf, len );
ggformat_impl( &fb, fmt, rest... );
return fb.len;
}
template< typename... Rest >
-bool ggprint_to_file( FILE * file, const char * fmt, Rest... rest ) {
+bool ggprint_to_file( FILE * file, const char * fmt, const Rest & ... rest ) {
char buf[ 4096 ];
FormatBuffer fb( buf, sizeof( buf ) );
ggformat_impl( &fb, fmt, rest... );
@@ -166,7 +166,7 @@ bool ggprint_to_file( FILE * file, const char * fmt, Rest... rest ) {
}
template< typename... Rest >
-bool ggprint( const char * fmt, Rest... rest ) {
+bool ggprint( const char * fmt, const Rest & ... rest ) {
return ggprint_to_file( stdout, fmt, rest... );
}
diff --git a/string_examples.cc b/string_examples.cc
@@ -20,7 +20,7 @@ public:
}
template< typename... Rest >
- str( const char * fmt, Rest... rest ) {
+ str( const char * fmt, const Rest & ... rest ) {
sprintf( fmt, rest... );
}
@@ -35,13 +35,13 @@ public:
}
template< typename... Rest >
- void sprintf( const char * fmt, Rest... rest ) {
+ void sprintf( const char * fmt, const Rest & ... rest ) {
size_t copied = ggformat( buf, N, fmt, rest... );
length = min( copied, N - 1 );
}
template< typename... Rest >
- void appendf( const char * fmt, Rest... rest ) {
+ void appendf( const char * fmt, const Rest & ... rest ) {
size_t copied = ggformat( buf + length, N - length, fmt, rest... );
length += min( copied, N - length - 1 );
}