Topic: Is this code valid in n3000?


Author: snk_kid <korcanh@googlemail.com>
Date: Sat, 23 Jan 2010 03:10:58 CST
Raw View
Taken from wikipedia:

"template<char...> OutputType operator "" _Suffix();

OutputType someVariable = "1234"_Suffix;

This instantiates the literal processing function as
operator""_Suffix<'1', '2', '3', '4'>. In this form, there is no
terminating null character to the string. The main purpose to doing
this is to use C++0x's constexpr keyword and the compiler to allow the
literal to be transformed entirely at compile time, assuming
OutputType is a constexpr-constructable and copyable type, and the
literal processing function is a constexpr function."

If this is legitimate then why restrict this to only user-defined
literals? why not allow them to work with any variadic template
functions? if this was possible we could have a better solution to
compile-time type-checked, type-safe format strings in C++:

"template <char... FChar, typename... Tp> // where (Showable Tp)
void print(Tp... args) {/* ... */}

print<"this: {0} and that: {1}">(1.0, Foo {...}); // Foo is some user-
defined type"

Without this ability and if what is said in wikipedia C++0x entry is
true I can think of doing something like making a special constexpr
format proxy type returned from user-defined literals:

template<char... FChar> constexpr  format<char... FChar> operator ""
_Fmt();

template <char... FChar, typename... Tp> // where (Showable Tp)
void print(format<FChar...> fmt, Tp... args) {/* ... */}

print("this: {0} and that: {1}" _Fmt, 1.0, Foo {...}); // Foo is some
user-defined type"

I guess that is actually looks better but you still have to remember
to use the suffix with your format string and I'm not 100% sure this
is possible without a working implementation of constexpr to write &
test out.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Sean Hunt <rideau3@gmail.com>
Date: Sun, 24 Jan 2010 00:47:55 CST
Raw View
On Jan 23, 2:10 am, snk_kid <korc...@googlemail.com> wrote:
> Taken from wikipedia:
>
> "template<char...> OutputType operator "" _Suffix();
>
> OutputType someVariable = "1234"_Suffix;

This is not legal, as the templated version is considered only for
integral and floating literals in n3000. I've updated the Wikipedia
article accordingly.

Sean


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]