カルマを使用して、文字列を提供するメンバー関数を含む構造体のベクトルから、文字列のコンマ区切りリストを生成しようとしています。
phoenix::bind を使用して単一の文字列出力を生成することも、文字列のベクトルから csv 出力を生成することもできますが、2 つのアプローチを組み合わせるのに苦労しています。
次の例では、最初のルールは正常に機能しますが、2 番目のルールはコンパイルされず (VS2013、boost 1.57 を使用)、フェニックス エラーが発生します: 「'std::vector> *' から 'foo *' に変換できません」。この場合、構造体の単一のインスタンスに対して機能するセマンティック アクションは正しくありません。融合アダプターを使用する唯一の答えはありますか?
#include <iostream>
#include <vector>
#include <string>
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace karma = boost::spirit::karma;
namespace phx = boost::phoenix;
struct foo
{
std::string f() const { return "bar"; }
};
int main()
{
std::string output;
typedef std::back_insert_iterator< std::string > iterator;
iterator it(output);
// this works:
std::vector<std::string> svec = { "foo", "bar", "baz" };
karma::rule< iterator, std::vector<std::string>() > svec_rule = karma::string % ',';
karma::generate(it, svec_rule, svec);
std::cerr << output << '\n';
// but this won't compile
std::vector<foo> vfoo(3);
karma::rule< iterator, std::vector<foo>&() > vfoo_rule = karma::string[karma::_1 = phx::bind(&foo::f, karma::_val)] % ',';
karma::generate(it, vfoo_rule, vfoo);
std::cerr << output << '\n';
}