so that I can print the m_ecuFirmwareVersion which was declared like so:
std::string m_ecuHardwareVersion;
here is my print method:
cout << printf("\nThe version is: %s", NovariantModule::getHardwareVersion().c_str());
it is printing 17 or 18 or sometimes 19 this value shouldn't be changing I know that so I am wondering if perhaps I am trying to print it incorrectly...
You're calling printf, and then printing the return value of printf to cout (printf returns the number of characters written).
You probably just want: cout << "\nThe version is: " << NovariantModule::getHardwareVersion() << '\n';
Still. if the number is changing, then that still most likely means that getHardwareVersion() is returning a different string than before. Are you absolutely sure it's not?
The following prints out the same thing for me each time:
// Example program
#include <iostream>
#include <cstdio>
#include <string>
namespace NovariantModule {
std::string getHardwareVersion()
{
return"1.2.3.4";
}
}
int main()
{
usingnamespace std;
cout << printf("\nThe version is: %s", NovariantModule::getHardwareVersion().c_str());
cout << printf("\nThe version is: %s", NovariantModule::getHardwareVersion().c_str());
cout << printf("\nThe version is: %s", NovariantModule::getHardwareVersion().c_str());
}
The version is: 1.2.3.424
The version is: 1.2.3.424
The version is: 1.2.3.424
Are there other libraries that provide similar functionality?
There are a few alternatives, most notably "fmt", which is dramatically superior to Boost.Format in terms of performance, code size, and compilation speed. https://fmt.dev/latest/index.html
Is fmt still "better" (I know, opinion) than using C++20's <format>?
I'd prefer to use the C++ standard version over a 3rd party library if I need printf-like formatting. Usually I don't need it. "Straight" stream output works for me.
As of C++20 <format> requires the programmer to pointlessly allocate a std::string just to print some characters on the screen, as in std::cout << std::format("{} {}\n", "hello", "world");
but if this is undesirable the programmer can use a ridiculous iterator-based interface like std::format_to(std::ostreambuf_iterator<char>{std::cout}, "{} {}\n", "hello", "world");
Contrast this with fmt which actually has a function that lets you print stuff to a stream.
Also fmt but not std::format can understand format strings at compile-time, and is therefore faster, in at least in theory.