Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is there a way to use literal-suffixes without selecting a namespace ?

40 views
Skip to first unread message

Bonita Montero

unread,
Mar 19, 2021, 3:21:34 AM3/19/21
to
Is there a way to use literal-suffixes without selecting a namespace ?
F.e. "1std::s" doesn't work when I didn't say using chrono. Is there
something similar that would work ?

Alf P. Steinbach

unread,
Mar 19, 2021, 7:30:45 AM3/19/21
to
Just

using namespace std::literals;

and be done with it. ;-)

- Alf

Bonita Montero

unread,
Mar 19, 2021, 9:07:11 AM3/19/21
to
>> Is there a way to use literal-suffixes without selecting a namespace ?
>> F.e. "1std::s" doesn't work when I didn't say using chrono. Is there
>> something similar that would work ?

> Just
>     using namespace std::literals;
> and be done with it. ;-)

As you can imagine I'm already aware of that.
But that's not what I asked for.

Alf P. Steinbach

unread,
Mar 19, 2021, 12:19:30 PM3/19/21
to
You could call the operator like a function, e.g.

std::literals::operator""s( "Blah" )

You could even create a local synonym:

auto s( const char* const s )
-> string
{ return std::literals::operator""s( s ); }

Using string literal example because I'm not so familiar with chrono.

Disclaimer: off the cuff code, not touched by compilers' dirty hands.

- Alf

Bonita Montero

unread,
Mar 19, 2021, 1:49:56 PM3/19/21
to
> You could call the operator like a function, e.g.
>     std::literals::operator""s( "Blah" )
> You could even create a local synonym:
>     auto s( const char* const s )
>         -> string
>     { return std::literals::operator""s( s ); }
> Using string literal example because I'm not so familiar with chrono.
> Disclaimer: off the cuff code, not touched by compilers' dirty hands.

As my question suggests I was asking for a nearly as convenient solution
than the example I gave, i.e. 1std::s f.e..

Juha Nieminen

unread,
Mar 19, 2021, 3:02:28 PM3/19/21
to
While the suffixes are cool and all, I myself just prefer to write eg.

std::chrono::seconds(123)

Sure, it's longer, but a lot easier to read and understand at a
quick glance.

Bonita Montero

unread,
Mar 19, 2021, 3:15:04 PM3/19/21
to
> While the suffixes are cool and all, I myself just prefer to write eg.
> std::chrono::seconds(123)
> Sure, it's longer, but a lot easier to read and understand at a
> quick glance.

I always select the std::/chrono::-namespaces in my .cpp-file globally.

Christian Gollwitzer

unread,
Mar 20, 2021, 3:23:19 AM3/20/21
to
Am 19.03.21 um 20:14 schrieb Bonita Montero:
How about importing only the suffixes, like "using std::chrono::s" ?
(untested)

Christian

Bonita Montero

unread,
Mar 20, 2021, 5:19:09 AM3/20/21
to
>> I always select the std::/chrono::-namespaces in my .cpp-file globally.

> How about importing only the suffixes, like "using std::chrono::s" ?
> (untested)

That's a matter of taste and too much work for me to import every
single entity.

Juha Nieminen

unread,
Mar 23, 2021, 2:45:08 AM3/23/21
to
I don't see any advantage in writing "seconds(123)" instead of
"std::chrono::seconds(123)". The latter makes it absolutely clear and
unambiguous what it is. The former is confusing, especially for someone
who is just casually reading the code for the first time.

I am not the kind of coder who has this strange obsession with making
everything as short as possible. I prefer clarity over brevity.
Overt brevity only introduces obfuscation.
0 new messages