Use variadic args to call subsequent method - c++17

Assume I have function that accept 1 callable F and variadic list of arguments:
template <class F, class ...Args>
void combine(F& f, Args&& ... args)
F may have any number of arguments from 1 to sizeof...(Args). So I need using c++17 to invoke F and after that just put not used args to std::cout like that
{
std::invoke(f, [some way to restrict args]);
std::cout << [The rest of args] << "\n";
}

The great problem is detect the number of arguments of F.
As pointed by Igor Tandetnik in a comment, this isn't possible in general, because the f func can be variadic, of a lambda with a template operator(), or a instance of a class/struct with multiple operator().
Anyway... in some cases you can detect the number of the arguments with something as follows
template <typename T>
struct num_args
: public num_args<decltype(&T::operator())>
{ };
template <typename R, typename ... Args>
struct num_args<R(*)(Args...)>
: public std::integral_constant<std::size_t, sizeof...(Args)>
{ };
// other specialization follows
At this point, you can write combine() to call an helper function with an appropriate couple of indexes and a tuple of arguments
template <typename F, typename ... Args>
void combine (F & func, Args && ... as)
{
constexpr auto n1 { num_args<F>::value };
constexpr auto n2 { sizeof...(Args) - n1 };
combine_helper(std::make_index_sequence<n1>{},
std::make_index_sequence<n2>{},
func,
std::forward_as_tuple(std::forward<Args>(as)...));
}
and the helper function can be simply as follows
template <std::size_t ... Is1, std::size_t ... Is2, typename F, typename T>
void combine_helper (std::index_sequence<Is1...>,
std::index_sequence<Is2...>,
F & func, T && t)
{
func(std::get<Is1>(std::forward<T>(t))...);
(std::cout << ... << std::get<sizeof...(Is1)+Is2>(std::forward<T>(t)))
<< '\n';
}
The following is a full compiling C++17 example
#include <iostream>
#include <utility>
#include <tuple>
template <typename T>
struct num_args
: public num_args<decltype(&T::operator())>
{ };
template <typename R, typename ... Args>
struct num_args<R(*)(Args...)>
: public std::integral_constant<std::size_t, sizeof...(Args)>
{ };
template <typename R, typename ... Args>
struct num_args<R(Args...)>
: public std::integral_constant<std::size_t, sizeof...(Args)>
{ };
template <typename R, typename C, typename ... Args>
struct num_args<R(C::*)(Args...)>
: public std::integral_constant<std::size_t, sizeof...(Args)>
{ };
template <typename R, typename C, typename ... Args>
struct num_args<R(C::*)(Args...) const>
: public std::integral_constant<std::size_t, sizeof...(Args)>
{ };
template <std::size_t ... Is1, std::size_t ... Is2, typename F, typename T>
void combine_helper (std::index_sequence<Is1...>,
std::index_sequence<Is2...>,
F & func, T && t)
{
func(std::get<Is1>(std::forward<T>(t))...);
(std::cout << ... << std::get<sizeof...(Is1)+Is2>(std::forward<T>(t)))
<< '\n';
}
template <typename F, typename ... Args>
void combine (F & func, Args && ... as)
{
constexpr auto n1 { num_args<F>::value };
constexpr auto n2 { sizeof...(Args) - n1 };
combine_helper(std::make_index_sequence<n1>{},
std::make_index_sequence<n2>{},
func,
std::forward_as_tuple(std::forward<Args>(as)...));
}
void func_1 (int a, int b, int c)
{ std::cout << "the product is: " << a*b*c << '\n'; }
int main()
{
auto extra { 100 };
auto func_2 { [&](int a, int b, int c, int d)
{ std::cout << "the extra sum is: " << extra+a+b+c+d << '\n'; } };
combine(func_1, 1, 2, 3, 4, 5, 6);
combine(func_2, 1, 2, 3, 4, 5, 6);
}

Related

FLEX/YACC program not behaving as expected : can't grab int value from sequence of ints

I am trying to build a parser that takes a list of strings in the following format and performs either an addition or multiplication of all of its elements :
prod 5-6_
sum _
sum 5_
sum 5-6-7_
$
Should print the following to the screen :
prod = 30
sum = 0
sum = 5
sum = 18
What I am actually getting as output is this :
prod = 0
sum = 0
sum = 5
sum = 5
My lex file looks like this :
%{
#include <iostream>
#include "y.tab.h"
using namespace std;
extern "C" int yylex();
%}
%option yylineno
digit [0-9]
integer {digit}+
operator "sum"|"prod"
%%
{integer} { return number; }
{operator} { return oper; }
"-" { return '-'; }
"_" { return '_'; }
"$" { return '$'; }
\n { ; }
[\t ]+ { ; }
. { cout << "unknown char" << endl; }
%%
and my yacc file looks like this :
%token oper
%token number
%token '-'
%token '_'
%token '$'
%start valid
%{
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
using namespace std;
#define YYSTYPE int
extern FILE *yyin;
extern char yytext[];
extern "C" int yylex();
int yyparse();
extern int yyerror(char *);
char op;
%}
%%
valid : expr_seq endfile {}
| {}
;
expr_seq : expr {}
| expr_seq expr {}
;
expr : op sequence nl {if (op == '+') cout << "sum = " ; else cout << "prod = ";}
| op nl {if (op == '+') cout << "sum = 0"; else cout <<"prod = 1";}
;
op : oper { if (yytext[0] == 's') op = '+'; else op = '*';}
;
sequence : number { $$ = atoi(yytext);}
| sequence '-' number { if (op == '+') $$ = $1 + $3; else $$ = $1 * $3;}
;
nl : '_' { cout << endl;}
;
endfile : '$' {}
;
%%
int main(int argc, char *argv[])
{
++argv, --argc;
if(argc > 0) yyin = fopen(argv[0], "r");
else yyin = stdin;
yyparse();
return 0;
}
int yyerror(char * msg)
{
extern int yylineno;
cerr << msg << "on line # " << yylineno << endl;
return 0;
}
My reasoning for the yacc logic is as follows :
a file is valid only if it contains a sequence of expressions followed by the endfile symbol.
a sequence of expressions is a single expression or several expressions.
an expression is either an operator followed by a new line, OR an operator, followed by a list of numbers, followed by a new line symbol.
an operator is either 'sum' or 'prod'
a list of numbers is either a number or several numbers separated by the '-' symbol.
From my perspective this should work, but for some reason it doesn't interpret the sequence of numbers properly after the first element. Any tips would be helpful.
Thanks
You must not use yytext in your yacc actions. yytext is only valid during a scanner action, and the parser often reads ahead to the next token. (In fact, yacc always reads the next token. Bison sometimes doesn't, but it's not always easily predictable.)
You can associate a semantic value with every token (and non-terminal), and you can reference these semantic values using $1, $2, etc. in your yacc actions. You can even associate semantic values of different types to different grammar symbols. And if you use bison -- and you probably are using bison -- you can give grammar symbols names to make it easier to refer to their semantic values.
This is all explained in depth, with examples, in the bison manual.
The solution that worked was simply to change the following lines :
sequence : number { $$ = atoi(yytext);}
| sequence '-' number { if (op == '+') $$ = $1 + $3; else $$ = $1 * $3;}
;
to this :
sequence : number { $$ = atoi(yytext);}
| sequence '-' number { if (op == '+') $$ = $1 + atoi(yytext); else $$ = $1 * atoi(yytext);}
;

Perfectly forwarding function wrapper?

We consider the following functor, to help to detect what version of of its operator() is called.
struct functor
{
void operator()() {std::cout << "functor::operator" << std::endl;}
void operator()() const {std::cout << "functor::operator const" << std::endl;}
void operator()() volatile {std::cout << "functor::operator volatile" << std::endl;}
void operator()() const volatile {std::cout << "functor::operator const volatile" << std::endl;}
void operator()(int) & {std::cout << "functor::operator &" << std::endl;}
void operator()(int) const& {std::cout << "functor::operator const&" << std::endl;}
void operator()(int) volatile& {std::cout << "functor::operator volatile&" << std::endl;}
void operator()(int) const volatile& {std::cout << "functor::operator const volatile&" << std::endl;}
void operator()(int) && {std::cout << "functor::operator &&" << std::endl;}
void operator()(int) const&& {std::cout << "functor::operator const&&" << std::endl;}
void operator()(int) volatile&& {std::cout << "functor::operator volatile&&" << std::endl;}
void operator()(int) const volatile&& {std::cout << "const volatile&&" << std::endl;}
};
I would like to know, in C++17 (if doable of course) how to write two functor wrappers storing the function in a tuple and using class template argument deduction:
The first one wrapper1 that will call the operator depending on the qualifiers of the functor
The second one wrapper2 that will call the operator depending on the qualifiers of the wrapper
For example:
wrapper1 w1(functor{});
wrapper2 w2(functor{});
w1(0); // should call the && version since the functor was built as a temporary
w2(0); // should call the & version since the wrapper was built as a value
Here are some very rough exploration of the kind of things I am looking for. Note: this is just to give a "taste" of what I am thinking of.
template <class F>
struct wrapper_a
{
constexpr wrapper_a(F f) noexcept: _tuple(f) {}
template <class... Args>
void operator()(Args&&... args)
{std::get<0>(_tuple)(std::forward<Args>(args)...);}
std::tuple<F> _tuple;
};
template <class F>
struct wrapper_b
{
template <class G>
constexpr wrapper_b(G&& g) noexcept: _tuple(std::forward<G>(g)) {}
template <class... Args>
void operator()(Args&&... args)
{std::get<0>(_tuple)(std::forward<Args>(args)...);}
std::tuple<F> _tuple;
};
template <class F> wrapper_b(F&& f) -> wrapper_b<F>;
template <class F>
struct wrapper_c
{
template <class G>
constexpr wrapper_c(G&& g) noexcept: _tuple(std::forward<G>(g)) {}
template <class... Args>
void operator()(Args&&... args)
{std::forward<F>(std::get<0>(_tuple))(std::forward<Args>(args)...);}
std::tuple<F> _tuple;
};
template <class F> wrapper_c(F&& f) -> wrapper_c<F>;
How to achieve what I am looking for? Is that even possible?
Not sure to understand your exactly requirements... and I'm a rookie with reference forwarding... anyway
The first one wrapper1 that will call the operator depending on the qualifiers of the functor
Maybe an expert can avoid this complication but seems to me that you need two template parameters
template <typename F, typename G>
struct wrapper1
where F is the type of the copy of the parameter passed to the constructor and G is the intended type.
So you have in it a F value
F f;
and you can use it trough forwarding
template <typename ... Args>
void operator() (Args && ... as) &
{ std::forward<G>(f)(std::forward<Args>(as)...); }
To simplify, you can define a template deduction as follows
template <typename F>
wrapper1(F && f) -> wrapper1<std::decay_t<F>, decltype(std::forward<F>(f))>;
The second one wrapper2 that will call the operator depending on the qualifiers of the wrapper
Maybe I'm wrong but this seems to me a littler simpler.
You need only a template parameter
template <typename F>
struct wrapper2
and you only have to use std::move() in r-reference operators
template <typename ... Args>
void operator() (Args && ... as) &&
{ std::move(f)(std::forward<Args>(as)...); }
The deduction guide is simple
template <typename F>
wrapper2(F && f) -> wrapper2<std::decay_t<F>>;
The following is a limited (to const/not-const and l-value/r-value reference alternatives) but full working examples.
Observe that there are some compilation errors caused by constness problem (if you initialize a const wrapper1 with a not-const callable, there are problems)
#include <iostream>
struct functor
{
void operator()(int) &
{std::cout << "functor::operator &" << std::endl;}
void operator()(int) const &
{std::cout << "functor::operator const &" << std::endl;}
void operator()(int) &&
{std::cout << "functor::operator &&" << std::endl;}
void operator()(int) const &&
{std::cout << "functor::operator const &&" << std::endl;}
};
template <typename F, typename G>
struct wrapper1
{
template <typename H>
constexpr wrapper1 (H && f0) noexcept : f{std::forward<H>(f0)}
{}
template <typename ... Args>
void operator() (Args && ... as) &
{ std::forward<G>(f)(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) const &
{ std::forward<G>(f)(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) &&
{ std::forward<G>(f)(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) const &&
{ std::forward<G>(f)(std::forward<Args>(as)...); }
F f;
};
template <typename F>
wrapper1(F && f)
-> wrapper1<std::decay_t<F>, decltype(std::forward<F>(f))>;
template <typename F>
struct wrapper2
{
template <typename H>
constexpr wrapper2 (H && f0) noexcept : f{std::forward<H>(f0)}
{}
template <typename ... Args>
void operator() (Args && ... as) &
{ f(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) const &
{ f(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) &&
{ std::move(f)(std::forward<Args>(as)...); }
template <typename ... Args>
void operator() (Args && ... as) const &&
{ std::move(f)(std::forward<Args>(as)...); }
F f;
};
template <typename F>
wrapper2(F && f) -> wrapper2<std::decay_t<F>>;
int main ()
{
functor fc;
functor const fd;
wrapper1 w1a{functor{}};
wrapper1 w1b{static_cast<functor const &&>(functor{})};
wrapper1 w1c{fc};
wrapper1 w1d{fd};
wrapper1 const w1e{functor{}};
wrapper1 const w1f{static_cast<functor const &&>(functor{})};
wrapper1 const w1g{fc};
wrapper1 const w1h{fd};
w1a(0);
w1b(0);
w1c(0);
w1d(0);
std::cout << "----------------------------" << std::endl;
// w1e(0); // compilation error
w1f(0);
// w1g(0); // compilation error
w1h(0);
std::cout << "----------------------------" << std::endl;
wrapper1<functor, functor&&>{functor{}}(0);
wrapper1<functor, functor const &&>
{static_cast<functor const &&>(functor{})}(0);
wrapper1<functor, functor&>{fc}(0);
wrapper1<functor, functor const &>{fd}(0);
std::cout << "----------------------------" << std::endl;
// (wrapper1 <functor, functor&&> const)
//{functor{}}(0); // compilation error
(wrapper1<functor, functor const &&> const)
{static_cast<functor const &&>(functor{})}(0);
// (wrapper1<functor, functor&> const){fc}(0); // compilation error
(wrapper1<functor, functor const &> const){fd}(0);
wrapper2 w2a{functor{}};
wrapper2 w2b{static_cast<functor const &&>(functor{})};
wrapper2 w2c{fc};
wrapper2 w2d{fd};
wrapper2 const w2e{functor{}};
wrapper2 const w2f{static_cast<functor const &&>(functor{})};
wrapper2 const w2g{fc};
wrapper2 const w2h{fd};
std::cout << "----------------------------" << std::endl;
w2a(0);
w2b(0);
w2c(0);
w2d(0);
std::cout << "----------------------------" << std::endl;
w2e(0);
w2f(0);
w2g(0);
w2h(0);
std::cout << "----------------------------" << std::endl;
wrapper2<functor>{functor{}}(0);
wrapper2<functor>{static_cast<functor const &&>(functor{})}(0);
wrapper2<functor>{fc}(0);
wrapper2<functor>{fd}(0);
std::cout << "----------------------------" << std::endl;
(wrapper2<functor> const){functor{}}(0);
(wrapper2<functor> const){static_cast<functor const &&>(functor{})}(0);
(wrapper2<functor> const){fc}(0);
(wrapper2<functor> const){fd}(0);
}

What is the C++ way of handling a framework to call functions of different types from a script?

I have tried different approaches and asked several specific questions regarding sub-problems of my requirement here. But my solution doesn't really work as expected, so I am taking a step back and ask here from a more general perspective. Please bear in mind that I am not a C++ pro. Not a beginner either, but I am still learning the language.
So, I have the following requirement. I need to read-in text files, which contain conditions, like "Greater" or "Equals", all like functions that return boolean values. The text files also include parameters for these conditions. Note that these parameters can be of different types (integer, decimal, etc.), and each such condition can take different number of parameters (e.g. "Equals" takes 2 paramters, while "Between" would take 3 parameters). Thus the file could look something like this:
Greater, 2, 3
Greater, 2.4, 1.0
Equals, true, true
Between, 20, 10, 30
The logic to read-in that file and parsing it is already done. Now I need to "concatenate" all these boolean functions with their parameters and check if all of them are true.
So I thought I would create functions or a class with static methods to represent these boolean test functions, then create a map of function pointers to these functions, mapped by their name. At runtime I would then read in the file, call the respective function pointer and pass-in the paramteres. That seemed easy to me, but in reality I am struggling mostly with the fact that these boolean functions can take a different number of parameters, and that these can be of different types.
Can you recommend a way to tackle that requirement in C++? I am not asking for a complete solution, but for a suitable C++ approach, or a guideline I could follow. Thanks in advance!
Here's a quick&dirty Spirit grammar for the input shown.
UPDATE
Now added invocation and implementation of the predicate functions (GreaterImpl and EqualsImpl).
I tried to be smart allowing comparisons between mixed arithmetic types (but not e.g. Greater(bool,string). If you compare incompatible types you will get a std::runtime_error exception that provides type feedback to the caller.
Live On Coliru
#include <deque>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/adapted/struct.hpp>
namespace qi = boost::spirit::qi;
namespace Ast {
using Value = boost::variant<int, double, bool, std::string>;
using BinaryPred = std::function<bool(Value, Value)>;
using TernaryPred = std::function<bool(Value, Value, Value)>;
using Pred = boost::variant<BinaryPred, TernaryPred>;
using Values = std::vector<Value>;
struct Invocation { Pred pred; Values args; };
using Invocations = std::vector<Invocation>;
}
BOOST_FUSION_ADAPT_STRUCT(Ast::Invocation, pred, args)
namespace Predicates {
using Ast::Value;
struct Greater : boost::static_visitor<bool> {
bool operator()(Value const& a, Value const& b) const {
return boost::apply_visitor(*this, a, b);
}
template <typename T> bool operator()(T const& a, T const& b) const { return std::greater<T>{}(a, b); }
template <typename T, typename U>
typename std::enable_if<std::is_arithmetic<T>() && std::is_arithmetic<U>(), bool>::type
operator()(T const& a, U const& b) const { return a > b; }
template <typename T, typename U>
typename std::enable_if<not (std::is_arithmetic<T>() && std::is_arithmetic<U>()), bool>::type
operator()(T const&, U const&) const { throw std::runtime_error("Type Mismatch"); }
};
struct Equals : boost::static_visitor<bool> {
bool operator()(Value const& a, Value const& b) const {
return boost::apply_visitor(*this, a, b);
}
template <typename T> bool operator()(T const& a, T const& b) const { return std::equal_to<T>{}(a, b); }
template <typename T, typename U, typename enable = typename std::enable_if<std::is_arithmetic<T>() && std::is_arithmetic<U>()>::type >
bool operator()(T const& a, U const& b) const { return a == b; }
template <typename T, typename U>
typename std::enable_if<not (std::is_arithmetic<T>() && std::is_arithmetic<U>()), bool>::type
operator()(T const&, U const&) const { throw std::runtime_error("Type Mismatch"); }
};
struct Between {
bool operator()(Value const& v, Value const& lower, Value const& upper) const {
return Greater{}(v,lower) && Greater{}(upper,v);
}
};
}
static inline bool evaluate(Ast::Invocation const& i) {
struct Invoker {
using result_type = bool;
Ast::Values const& args;
result_type operator()(Ast::BinaryPred const& p) const {
if (args.size() != 2) throw std::runtime_error("Arity Mismatch");
return p(args.at(0), args.at(1));
}
result_type operator()(Ast::TernaryPred const& p) const {
if (args.size() != 3) throw std::runtime_error("Arity Mismatch");
return p(args.at(0), args.at(1), args.at(2));
}
};
return boost::apply_visitor(Invoker{i.args}, i.pred);
}
template <typename It>
struct Grammar : qi::grammar<It, Ast::Invocations()> {
Grammar() : Grammar::base_type(start) {
using namespace qi;
start = skip(blank) [ invocation % eol ];
invocation = pred >> -("," >> args);
args = arg % ",";
arg = my_double_ | qi::int_ | qi::bool_ | lexeme['"' > *~char_('"') > '"'];
}
private:
struct pred_t : qi::symbols<char, Ast::Pred> {
pred_t() {
this->add
("Greater", Predicates::Greater{})
("Equals", Predicates::Equals{})
("Between", Predicates::Between{})
;
}
} const pred;
qi::rule<It, Ast::Invocations()> start;
qi::rule<It, Ast::Invocation(), qi::blank_type> invocation;
qi::rule<It, Ast::Values(), qi::blank_type> args;
qi::rule<It, Ast::Value(), qi::blank_type> arg;
qi::real_parser<double, qi::strict_real_policies<double> > my_double_;
};
#include <sstream>
int main() {
using It = boost::spirit::istream_iterator;
std::deque<std::string> testcases {
// one multiline case:
"Between, 20, 10, 30\n"
"Between, NaN, NaN, NaN\n"
"Between, \"q\", \"a\", \"z\""
};
// many single line cases for easy test reporting
for (std::string op : {"Greater","Equals"})
for (auto rhs : { "42", "0.0", "true", "\"hello\"" })
for (auto lhs : { "41", "-0.0", "false", "\"bye\"" }) {
testcases.push_front(op + ", " + lhs + ", " + rhs);
}
for (auto testcase : testcases) {
std::cout << "--- Testcase '" << testcase << "' -> ";
std::istringstream iss(testcase);
It f(iss >> std::noskipws), l;
Ast::Invocations parsed;
if (qi::parse(f, l, Grammar<It>(), parsed)) {
for (auto& invocation : parsed) {
try {
std::cout << std::boolalpha << evaluate(invocation) << "; ";
} catch(std::exception const& e) {
std::cout << e.what() << "; ";
}
}
std::cout << "\n";
} else {
std::cout << "Parse failed\n";
}
if (f != l)
std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}
}
Prints:
--- Testcase 'Equals, "bye", "hello"' -> false;
--- Testcase 'Equals, false, "hello"' -> Type Mismatch;
--- Testcase 'Equals, -0.0, "hello"' -> Type Mismatch;
--- Testcase 'Equals, 41, "hello"' -> Type Mismatch;
--- Testcase 'Equals, "bye", true' -> Type Mismatch;
--- Testcase 'Equals, false, true' -> false;
--- Testcase 'Equals, -0.0, true' -> false;
--- Testcase 'Equals, 41, true' -> false;
--- Testcase 'Equals, "bye", 0.0' -> Type Mismatch;
--- Testcase 'Equals, false, 0.0' -> true;
--- Testcase 'Equals, -0.0, 0.0' -> true;
--- Testcase 'Equals, 41, 0.0' -> false;
--- Testcase 'Equals, "bye", 42' -> Type Mismatch;
--- Testcase 'Equals, false, 42' -> false;
--- Testcase 'Equals, -0.0, 42' -> false;
--- Testcase 'Equals, 41, 42' -> false;
--- Testcase 'Greater, "bye", "hello"' -> false;
--- Testcase 'Greater, false, "hello"' -> Type Mismatch;
--- Testcase 'Greater, -0.0, "hello"' -> Type Mismatch;
--- Testcase 'Greater, 41, "hello"' -> Type Mismatch;
--- Testcase 'Greater, "bye", true' -> Type Mismatch;
--- Testcase 'Greater, false, true' -> false;
--- Testcase 'Greater, -0.0, true' -> false;
--- Testcase 'Greater, 41, true' -> true;
--- Testcase 'Greater, "bye", 0.0' -> Type Mismatch;
--- Testcase 'Greater, false, 0.0' -> false;
--- Testcase 'Greater, -0.0, 0.0' -> false;
--- Testcase 'Greater, 41, 0.0' -> true;
--- Testcase 'Greater, "bye", 42' -> Type Mismatch;
--- Testcase 'Greater, false, 42' -> false;
--- Testcase 'Greater, -0.0, 42' -> false;
--- Testcase 'Greater, 41, 42' -> false;
--- Testcase 'Between, 20, 10, 30
Between, NaN, NaN, NaN
Between, "q", "a", "z"' -> true; false; true;
boost variant is the easiest way IMHO:
#include <boost/variant.hpp>
#include <boost/operators.hpp>
#include <string>
#include <iostream>
#include <iomanip>
// define the concept of equality in my scripting language
struct is_equal : boost::static_visitor<bool>
{
// x == x is easy
template<class T>
bool operator()(const T& l, const T& r) const {
return l == r;
}
// define the concept of comparing strings to integers
bool operator()(const std::string& l, const int& r) const {
return l == std::to_string(r);
}
// and integers to strings
bool operator()(const int& l, const std::string& r) const {
return (*this)(r, l);
}
};
struct is_less : boost::static_visitor<bool>
{
// x == x is easy
template<class T>
bool operator()(const T& l, const T& r) const {
return l < r;
}
// define the concept of comparing strings to integers
bool operator()(const std::string& l, const int& r) const {
return std::stoi(l) < r;
}
// and integers to strings
bool operator()(const int& l, const std::string& r) const {
return l < std::stoi(r);
}
};
struct emit : boost::static_visitor<std::ostream&>
{
emit(std::ostream& os) : os_(os) {}
// x == x is easy
template<class T>
std::ostream& operator()(const T& l) const {
return os_ << l;
}
std::ostream& operator()(const std::string& s) const {
return os_ << std::quoted(s);
}
std::ostream& os_;
};
struct scriptable_value
: boost::less_than_comparable<scriptable_value>
, boost::equality_comparable<scriptable_value>
{
using variant_type = boost::variant<std::string, int>;
scriptable_value(std::string v) : variant_(std::move(v)) {}
scriptable_value(int v) : variant_(v) {}
variant_type const& as_variant() const {
return variant_;
}
private:
variant_type variant_;
};
bool operator==(scriptable_value const& l, scriptable_value const& r)
{
return boost::apply_visitor(is_equal(), l.as_variant(), r.as_variant());
}
bool operator<(scriptable_value const& l, scriptable_value const& r)
{
return boost::apply_visitor(is_less(), l.as_variant(), r.as_variant());
}
std::ostream& operator<<(std::ostream& os, scriptable_value const& r)
{
return boost::apply_visitor(emit(os), r.as_variant());
}
int main()
{
auto x = scriptable_value(10);
auto y = scriptable_value("10");
auto x2 = scriptable_value(9);
auto y2 = scriptable_value("9");
std::cout << x << " == " << y << " : " << std::boolalpha << (x == y) << std::endl;
std::cout << x << " != " << y << " : " << std::boolalpha << (x != y) << std::endl;
std::cout << x << " == " << y2 << " : " << std::boolalpha << (x == y2) << std::endl;
std::cout << x << " != " << y2 << " : " << std::boolalpha << (x != y2) << std::endl;
std::cout << x << " < " << y << " : " << std::boolalpha << (x < y) << std::endl;
std::cout << x << " >= " << y << " : " << std::boolalpha << (x >= y) << std::endl;
std::cout << x << " < " << y2 << " : " << std::boolalpha << (x < y2) << std::endl;
std::cout << x << " >= " << y2 << " : " << std::boolalpha << (x >= y2) << std::endl;
std::cout << x << " == " << x2 << " : " << std::boolalpha << (x == x2) << std::endl;
std::cout << x << " != " << x2 << " : " << std::boolalpha << (x != x2) << std::endl;
}
expected output:
10 == "10" : true
10 != "10" : false
10 == "9" : false
10 != "9" : true
10 < "10" : false
10 >= "10" : true
10 < "9" : false
10 >= "9" : true
10 == 9 : false
10 != 9 : true
Your fundamental issue is that C++ is a statically-typed language. General-purpose programming languages tend to fall into two rough categories: statically and dynamically typed. In a dynamically-typed language, like Perl, the type of an object is determined at runtime. In a statically-typed language, like C++, the type of an object is specified at compile-time.
That doesn't mean that this is not possible in C++ in a type-safe manner. It is, but it requires some work.
The usual approach is to encapsulate all types into classes that are derived from some base class that defines virtual methods, with the subclasses implementing them. Let's use just ints and floats.
// Forward declarations
class FloatNumber;
class IntNumber;
class Number {
// virtual methods to be defined later.
};
class FloatNumber : public Number {
float value;
// Implements the virtual methods for float values.
};
class IntNumber : public Number {
int value;
// Implements the virtual methods for int values.
};
Now, you can implement basic operations. In the Number base class, you define the conversion methods:
virtual FloatNumber asFloat() const = 0;
virtual IntNumber asInt() const = 0;
In each subclass you will implement these in the obvious manner, returning either *this, if it's the same type, or constructing the other subclass and returning the newly-constructed class.
Now, you can implement basic operations. Say, equals:
virtual bool equals(const Number &other) const =0;
Now, you get to implement this virtual method in each subclass. In FloatNumber::equals(), for example, you would call other.asFloat(), and compare its val to its own val. Ditto for IntNumber::equals(). If both Numbers that are compared are of the same type, this ends up comparing the two values directly; otherwise an automatic type conversion takes place.
Now, this is not a perfect approach, since if the first number is an IntNumber, the FloatNumber ends up getting down-converted to an int, and you really want the conversion to go the other way. There are classical ways to solve this also, in a type-safe manner. But first, you should get this basic approach implemented first, and then worry about handling the various corner-cases.
In this manner you can proceed and build up a class hierarchy that implements generic operation on numbers. This is likely more work than you expected, but this is how to properly do this kind of thing in C++, in a completely type-safe manner. Modern C++ compilers are quite efficient, and the end result will be quite small, and compact.
The final step for you is to read the file, parse the values, and have a simple lookup table that maps "Equals" to the "equals" method, and so on...

How do you use boxed closures in FnMut contexts?

How can you use a boxed closure in a context that requires a FnMut type, e.g.
pub fn main() {
for n in (0..10).map(Box::new(|i| i * 2)) {
println!("{}", n);
}
}
As Box implements the Deref trait, you can simply derefence your boxed function:
fn main() {
let boxed_fn = Box::new(|i| i * 2);
for n in (0..10).map(*boxed_fn) {
println!("{}", n);
}
}

How can I implement #include constructs with Flex and YACC?

During parsing, if I encounter a include token I want to instruct YACC to open the file specified as input and to begin parsing this. Once this parsing is finished, I want to instruct YACC to return to the file and continue parsing directly after the include expression. I will restrict the include depth level to one.
The flex manual covers how to do this using yypush_buffer_state() and yypop_buffer_state().
Here is the section of the official manual on using multiple input buffers. There is some sample code.
It's normal to communicate between the lexical and syntactic phases of your processor.
So, recognize the syntax for an include directive in your parser (or, to make things easier, just recognize it in the lexer) and do the switching at the lexical level.
For example, here is a simple language that recognizes standard input lines containing ab or cd or .file. When it sees .someString it opens someString as an include file and then goes back to reading standard input.
%{
#include <stdio.h>
#include <stdlib.h>
void start_include(char *); int yylex(void); void yyerror(void *);
#define YYSTYPE char *
%}
%%
all: all others | others;
others: include_rule | rule_1 | rule_2 | 'Z' { YYACCEPT; };
include_rule: '.' '\n' { start_include($1); };
rule_1: 'a' 'b' '\n' { printf("random rule 1\n"); };
rule_2: 'c' 'd' '\n' { printf("random rule 2\n"); };
%%
FILE * f = NULL;
void start_include(char *s) {
if ((f = fopen(s, "r")) == NULL)
abort();
}
int yylex(void) {
int c;
static char s[100];
if (f == NULL)
f = stdin;
c = getc(f);
if (c == EOF) {
f = stdin;
c = getc(f);
}
if (c == '.') {
scanf(" %s", s);
yylval = s;
} else if (c == EOF)
return 'Z';
return c;
}
And when we run it...
$ cat > toplevel
ab
.myinclude
ab
$ cat > myinclude
cd
cd
$ yacc ip.y && cc -Wall y.tab.c -ly && ./a.out < toplevel
random rule 1
random rule 2
random rule 2
random rule 1
$

Resources