Skip to content

Commit 2f9335a

Browse files
authored
Allow formatting of volatile variables. (#62)
1 parent 705e3f4 commit 2f9335a

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

tinyformat.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,9 @@ class FormatArg
506506

507507
template<typename T>
508508
FormatArg(const T& value)
509-
: m_value(static_cast<const void*>(&value)),
509+
// C-style cast here allows us to also remove volatile; we put it
510+
// back in the *Impl functions before dereferencing to avoid UB.
511+
: m_value((const void*)(&value)),
510512
m_formatImpl(&formatImpl<T>),
511513
m_toIntImpl(&toIntImpl<T>)
512514
{ }

tinyformat_test.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ int unitTests()
286286

287287
//------------------------------------------------------------
288288
// Misc
289+
volatile int i = 1234;
290+
CHECK_EQUAL(tfm::format("%d", i), "1234");
291+
289292
#ifdef TEST_WCHAR_T_COMPILE
290293
// Test wchar_t handling - should fail to compile!
291294
tfm::format("%ls", L"blah");

0 commit comments

Comments
 (0)