Topic: On user-defined literals


Author: Magnus F<magfr@lysator.liu.se>
Date: Fri, 11 Nov 2011 13:05:06 -0800 (PST)
Raw View
Hello.

I would like to know how 13.5.8/3 is to be interpreted.

It says:

The declaration of a literal operator shall have a
parameter-declaration-clause equivalent to one of the
following:
...
const char*, std::size_t
...

What does 'equivalent to' mean? Is

template<std:size_t i>
int operator "" _x (char (&)[i], std::size_t)

equivalent enough for this code? (The point of the above is of course
to
turn the length into a constant expression)



I also have to say that it is sad that user-defined-character-
literal's
lack a raw form as that would be quite useful.

Consider

'xyz'_x

When calling operator "" _x (char) I think it is implementation-
defined
what the value of the argument is.

When calling operator "" _x (char*) the value of the argument would be
"xyz" everywhere and that is a lot more useful.

In the same way one could add a raw form for
user-defined-string-literal's, but that one is not as obviously
useful.

/MF


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]




Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Sat, 12 Nov 2011 11:34:17 -0800 (PST)
Raw View
Am 11.11.2011 22:05, schrieb Magnus F:
>
> Hello.
>
> I would like to know how 13.5.8/3 is to be interpreted.
>
> It says:
>
> The declaration of a literal operator shall have a
> parameter-declaration-clause equivalent to one of the
> following:
> ...
> const char*, std::size_t
> ...
>
> What does 'equivalent to' mean?

For example, instead of char, you could write const char or for const
char* you could write const char[42]. The meaning is defined in 8.3.5,
in particular in p4 and p5. The wording could be a bit more precise,
but I think from the context it is clear.

> Is
>
> template<std:size_t i>
> int operator "" _x (char (&)[i], std::size_t)
>
> equivalent enough for this code? (The point of the above is of course
> to turn the length into a constant expression)

Nope.

a) char (&)[i] is not equivalent to const char* as a
parameter-declaration-clause.

b) Above would be literal operator template, not a literal operator.
But according to 13.5.8 p5 the signature is not valid for a literal
operator template.

> I also have to say that it is sad that user-defined-character-
> literal's lack a raw form as that would be quite useful.
>
> Consider
>
> 'xyz'_x
>
> When calling operator "" _x (char) I think it is implementation-
> defined what the value of the argument is.

Yes.

> When calling operator "" _x (char*) the value of the argument would be
> "xyz" everywhere and that is a lot more useful.

I assume you mean  operator "" _x (const char*), but I fail to see the
advantage of supporting multicharacter literals. Why not using a
normal user-defined-string-literal instead?

HTH & Greetings from Bremen,

Daniel Kr   gler



--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]