Sujet : Re: Tabs As Syntax
De : OFeem1987 (at) *nospam* teleworm.us (Chris Ahlstrom)
Groupes : comp.os.linux.advocacyDate : 24. Mar 2024, 14:43:15
Autres entêtes
Organisation : None
Message-ID : <utp755$c5vq$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : slrn/1.0.3 (Linux)
DFS wrote this copyrighted missive and expects royalties:
On 3/23/2024 8:11 AM, Chris Ahlstrom wrote:
>
te<typename ... Args>
std::string string_format (const std::string & format, Args ... args)
{
std::string result;
size_t sz = std::snprintf(nullptr, 0, format.c_str(), args ...);
if (sz > 0)
{
std::unique_ptr<char []> buf(new char[sz + 1]);
std::snprintf(buf.get(), sz + 1, format.c_str(), args ...);
result = std::string(buf.get(), buf.get() + sz);
}
return result;
}
Note the variadic template.
Heh, didn't notice that "template" got chopped to "te". Fumble-fingers!
I note the two thousand uses of std::.
Actually, I left out one for the size_t declaration: std::size_t.
Couldn't you just use 'using namespace std;' inside this function (or
whatever this 'te' thing is?)
^ template DOH!
Like Larry said about using them in headers. But I will use them in headers in
this manner:
namespace foo
{
using container = std::vector<std::byte>;
class bar
{
using container = std::map<std::string, std::string>;
. . .
};
}
So the caller can choose to use foo::container or foo::bar::container.
If all those namespace bug you, you will surely hate the Boost libraries.
As I recall, it has nested namespaces galore.
One other note: I never use "using namespace std", even in the cpp file.
Also the same with namespaces used in other libraries, including my own
libraries. Better to show them upfront to avoid puzzlement.
And I don't do that in main(), either... always specify the namespace.
Adds clarity (and clutter, I will admit).
-- Q: What do Winnie the Pooh and John the Baptist have in common?A: The same middle name.