c++: std::to_string is available in C++11

Reported by Victor Khomenko.
http://lists.gnu.org/archive/html/bug-bison/2018-10/msg00033.html

* doc/bison.texi, examples/c++/variant-11.yy: Use std::to_string
instead of ours.
This commit is contained in:
Akim Demaille
2018-10-24 18:49:04 +02:00
parent 8fd5f0d5a5
commit 5b879c8980
2 changed files with 23 additions and 53 deletions

View File

@@ -10672,48 +10672,8 @@ Variants}):
%define api.value.type variant
@end example
Our list of strings will be built from two types of items: numbers and
strings:
@comment file: c++/simple.yy: 2
@example
%type <std::string> item;
%token <std::string> TEXT;
%token <int> NUMBER;
@group
item:
TEXT
| NUMBER @{ $$ = to_string ($1); @}
;
@end group
@end example
In the case of @code{TEXT}, the implicit default action applies: @w{@code{$$
= $1}.} We recommend that you keep the actions simple, and move details
into auxiliary functions, as we did with @code{to_string}, which we
implement in the prologue as follows:
@comment file: c++/simple.yy: 1
@example
%code
@{
#include <sstream>
@group
// Convert to string.
template <typename T>
auto to_string (const T& t) -> std::string
@{
std::ostringstream o;
o << t;
return o.str ();
@}
@end group
@}
@end example
Obviously, the rule for @code{result} needs to print a vector of strings.
Again, in the prologue, we add:
In the prologue, we add:
@comment file: c++/simple.yy: 1
@example
@@ -10740,7 +10700,27 @@ Again, in the prologue, we add:
@noindent
You may want to move it into the @code{yy} namespace to avoid leaking it in
your default namespace.
your default namespace. We recommend that you keep the actions simple, and
move details into auxiliary functions, as we did with @code{operator<<}.
Our list of strings will be built from two types of items: numbers and
strings:
@comment file: c++/simple.yy: 2
@example
%type <std::string> item;
%token <std::string> TEXT;
%token <int> NUMBER;
@group
item:
TEXT
| NUMBER @{ $$ = std::to_string ($1); @}
;
@end group
@end example
In the case of @code{TEXT}, the implicit default action applies: @w{@code{$$
= $1}.}
@sp 1

View File

@@ -69,16 +69,6 @@
// std::make_unique is C++14.
return string_uptr (new std::string{std::forward<Args> (args)...});
}
// Convert to string.
template <typename T>
std::string
to_string (const T& t)
{
auto&& o = std::ostringstream{};
o << t;
return o.str ();
}
}
%token <string_uptr> TEXT;
@@ -103,7 +93,7 @@ list:
item:
TEXT
| NUMBER { $$ = make_string_uptr (to_string ($1)); }
| NUMBER { $$ = make_string_uptr (std::to_string ($1)); }
;
%%