mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-18 00:33:03 +00:00
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:
@@ -10672,48 +10672,8 @@ Variants}):
|
|||||||
%define api.value.type variant
|
%define api.value.type variant
|
||||||
@end example
|
@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.
|
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
|
@comment file: c++/simple.yy: 1
|
||||||
@example
|
@example
|
||||||
@@ -10740,7 +10700,27 @@ Again, in the prologue, we add:
|
|||||||
|
|
||||||
@noindent
|
@noindent
|
||||||
You may want to move it into the @code{yy} namespace to avoid leaking it in
|
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
|
@sp 1
|
||||||
|
|
||||||
|
|||||||
@@ -69,16 +69,6 @@
|
|||||||
// std::make_unique is C++14.
|
// std::make_unique is C++14.
|
||||||
return string_uptr (new std::string{std::forward<Args> (args)...});
|
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;
|
%token <string_uptr> TEXT;
|
||||||
@@ -103,7 +93,7 @@ list:
|
|||||||
|
|
||||||
item:
|
item:
|
||||||
TEXT
|
TEXT
|
||||||
| NUMBER { $$ = make_string_uptr (to_string ($1)); }
|
| NUMBER { $$ = make_string_uptr (std::to_string ($1)); }
|
||||||
;
|
;
|
||||||
%%
|
%%
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user