Simple trait usage
#include <catch2/catch_test_macros.hpp>
#include <string>
namespace simple {
TRAIT(trait1, anyxx::dynamic_copyable, (FN(std::string, fn1, (), const)))
std::string cat(std::vector<anyxx::any<trait1>> const& items) {
std::string result;
for (auto const& item : items) {
result += item.fn1();
}
return result;
}
struct model1 {
std::string s;
[[nodiscard]] std::string fn1() const { return "model1"; }
};
struct model2 {
int x;
};
}
TRAIT_MAP(simple::trait1, simple::model2) {
static std::string fn1(simple::model2 const& m) {
return std::to_string(m.x);
};
};
TEST_CASE("any_ via concept simple") {
using namespace anyxx;
using namespace simple;
any<trait1> test1{model1{"model1"}};
CHECK(test1.fn1() == "model1");
any<trait1> test2{model2{42}};
CHECK(test2.fn1() == "42");
CHECK(cat({test1, test2}) == "model142");
}
C++ header only library for external polymorphism.