Używanie std::bind jako callable w mapie.

0

Cześć. taki mam kodzik i takie z nim problemy

Validator to po prostu struct:

template <typename F>
struct validator{
	std::regex reg;
	F isValid;
};

Robię 4 część challengu adventofcode (https://adventofcode.com/2020/day/4). Generalnie już ją zrobiłem, innym kodem, ale wychodzę z założenia, że jeśli mam zadanie i mam z czymś problem, i nie znam rozwiązania, to nawet jeśli napiszę inny kod rozwiązujacy te zadanie, tak czy siak powinienem rozwiązanie problemu.

Jest to druga część tego wyzwania, która brzmi tak:

The line is moving more quickly now, but you overhear airport security talking about how passports with invalid data are getting through. Better add some data validation, quick!

You can continue to ignore the cid field, but each other field has strict rules about what values are valid for automatic validation:

byr (Birth Year) - four digits; at least 1920 and at most 2002.
iyr (Issue Year) - four digits; at least 2010 and at most 2020.
eyr (Expiration Year) - four digits; at least 2020 and at most 2030.
hgt (Height) - a number followed by either cm or in:
    If cm, the number must be at least 150 a nd at most 193.
    If in, the number must be at least 59 and at most 76.
hcl (Hair Color) - a # followed by exactly six characters 0-9 or a-f.
ecl (Eye Color) - exactly one of: amb blu brn gry grn hzl oth.
pid (Passport ID) - a nine-digit number, including leading zeroes.
cid (Country ID) - ignored, missing or not.

Your job is to count the passports where all required fields are both present and valid according to the above rules. Here are some example values:

byr valid: 2002
byr invalid: 2003

hgt valid: 60in
hgt valid: 190cm
hgt invalid: 190in
hgt invalid: 190

hcl valid: #123abc
hcl invalid: #123abz
hcl invalid: 123abc

ecl valid: brn
ecl invalid: wat

pid valid: 000000001
pid invalid: 0123456789

Here are some invalid passports:

eyr:1972 cid:100
hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926

iyr:2019
hcl:#602927 eyr:1967 hgt:170cm
ecl:grn pid:012533040 byr:1946

hcl:dab227 iyr:2012
ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277

hgt:59cm ecl:zzz
eyr:2038 hcl:74454a iyr:2023
pid:3556412378 byr:2007

Here are some valid passports:

pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980
hcl:#623a2f

eyr:2029 ecl:blu cid:129 byr:1989
iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm

hcl:#888785
hgt:164cm byr:2001 iyr:2015 cid:88
pid:545766238 ecl:hzl
eyr:2022

iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719

Count the number of valid passports - those that have all required fields and valid values. Continue to treat cid as optional. In your batch file, how many passports are valid?

Plan jest taki:
Tworzę mapę nazwa atrybutu -> walidator (regex + właściwy walidator w postaci funkcji)
Sprawdzam czy każdy atrybut zgadza się z formatem
Jeśli się zgadza, to poddaję go walidacji przypisaną funkcją (3 funkcje zwracają true, ponieważ atrybut jest poprawny już jeśli pasuje do formatu, pozostałe sprawdzają "argumenty" atrybutu)
Zależnie od wyników zwracam true/false

Jednak jest coś nie tak z tym bindem i nie do końca wiem co z tym zrobić. VSC pokazuje mi dwa błędy:

  1. przy mapie

    brak odpowiedniej konwersji elementu "std::_Binder<std::_Unforced, type &, int, int>" na "std::_Binder<std::_Unforced, type &, const std::_Ph<1> &, const std::_Ph<2> &>" zdefiniowanej przez użytkownikaC/C++(312)

  2. przy vtorFunc(...):

    żadne wystąpienie elementu funkcja przeciążona "std::_Binder<_Ret, _Fx, _Types...>::operator() [gdzie _Ret=std::_Unforced, _Fx=type &, _Types=<const std::_Ph<1> &, const std::_Ph<2> &>]" nie jest zgodne z listą argumentów -- typy argumentów: (std::smatch) -- typ obiektu to: std::_Binder<std::_Unforced, type &, const std::_Ph<1> &, const std::_Ph<2> &>C/C++(304)

I szczerze nie mam pojęcia jak to poprawnie zrobić. Prosiłbym o pomoc.

bool validAttr(const std::string& attr){
    using namespace std::literals::string_literals;
	using namespace std::placeholders;

	auto toUnsigned = [](std::ssub_match match){
		unsigned ret{};
		auto matchStr = match.str();
		auto [ptr, err] = std::from_chars(matchStr.data(), matchStr.data()+matchStr.size(), ret);

		if(!(std::errc() == err)) throw std::runtime_error("Cannot convert "+matchStr+" to unsigned integer value.\n");

		return ret;
	};
	auto checkBounds = [](unsigned num, unsigned low, unsigned high){
		return low <= num && num <= high;
	};
	auto boundChecker = [&checkBounds, &toUnsigned](const std::smatch& sm, unsigned low, unsigned high){
			return checkBounds(toUnsigned(sm[1]), low, high);
	};
	auto retTrue = [](){ return true; };

	auto heightValidator = [&checkBounds, &toUnsigned](const std::smatch& sm){
			if("cm" == sm[2]) return checkBounds(toUnsigned(sm[1]), 150, 193);
			else if("in" == sm[2]) return checkBounds(toUnsigned(sm[1]), 59, 76);
			else return false;
	};

	using boundValidator_t = decltype(std::bind(boundChecker, _1, _2));
	using defValidator_t = decltype(retTrue);
	using heightValidator_t = decltype(heightValidator);

	using validator_v = std::variant<validator<boundValidator_t>, validator<defValidator_t>, validator<heightValidator_t>>;

	std::unordered_map<std::string, validator_v> validators{
		{"byr"s, validator<boundValidator_t>{std::regex{"byr:(\\d{4})"}, std::bind(boundChecker, 1920, 2002)}},
		{"iyr"s, validator<boundValidator_t>{std::regex{"iyr:(\\d{4})"}, std::bind(boundChecker, 2010, 2020)}},
		{"eyr"s, validator<boundValidator_t>{std::regex{"eyr:(\\d{4})"}, std::bind(boundChecker, 2020, 2030)}},
		{"hgt"s, validator<heightValidator_t>{std::regex{"hgt:(\\d{2,3})(in|cm)"}, heightValidator}},
		{"hcl"s, validator<defValidator_t>{std::regex{"hcl:#([0-9a-f]{6})"}, retTrue}},
		{"ecl"s, validator<defValidator_t>{std::regex{"ecl:#(amb|blu|brn|gry|grn|hzl|oth)"}, retTrue}},
		{"pid"s, validator<defValidator_t>{std::regex{"pid:\\d{9}"}, retTrue}}
	};

	auto attrName = attr.substr(0, 3);
	auto returnVisitor = [](const auto& validator){return validator};

	auto reg = std::visit(returnVisitor, validators[attrName]).reg;
	auto vtorFunc = std::visit(returnVisitor, validators[attrName]).isValid;

	std::smatch match;
	if(std::regex_match(attr, match, reg)){
		if(vtorFunc(match)) return true;
		return false;
	}else return false;
}
0

Każda lambda ma osobny typ, to jest na pewno problematyczne. Poza tym, std::bind jest odradzanym rozwiązaniem, skoro mamy lambdy.

Jeśli chodzi o błąd:

2_map.cpp:54:77: error: could not convert 'std::bind(_Func&&, _BoundArgs&& ...) [with _Func = validAttr(const string&)::<lambda(const smatch&, unsigned int, unsigned int)>&; _BoundArgs = {int, int}; typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type = std::_Bind_helper<false, validAttr(const string&)::<lambda(const smatch&, unsigned int, unsigned int)>&, int, int>::type](2020, 2030)' from '_Bind<validAttr(const string&)::<lambda(const smatch&, unsigned int, unsigned int)>(int, int)>' to '_Bind<validAttr(const string&)::<lambda(const smatch&, unsigned int, unsigned int)>(std::_Placeholder<1>, std::_Placeholder<2>)>'

Kompilator wyraźnie mówi, że mu nie pasują te funkcje:

ma:
_Bind<validAttr(const string&)::<lambda(const smatch&, unsigned int, unsigned int)>(int, int)>
oczekuje:
_Bind<validAttr(const string&)::<lambda(const smatch&, unsigned int, unsigned int)>(std::_Placeholder<1>, std::_Placeholder<2>)>

wniosek:

- using boundValidator_t = decltype(std::bind(boundChecker, _1, _2));
+ using boundValidator_t = decltype(std::bind(boundChecker, 0, 0));

Jeśli dalej masz problem, najlepiej gdybyś podzielił się MCVE i sprowadził zagadnienie do pierwszego elementu, który np. po odkomentowaniu sprawia, że kompilacja nie powodzi się.

0
#include <iostream>
#include <string>
#include <charconv>
#include <unordered_map>
#include <regex>
#include <functional>
#include <variant>

bool toUnsigned (std::ssub_match match){
		unsigned ret{};
		auto matchStr = match.str();
		auto [ptr, err] = std::from_chars(matchStr.data(), matchStr.data()+matchStr.size(), ret);

		if(!(std::errc() == err)) throw std::runtime_error("Cannot convert "+matchStr+" to unsigned integer value.\n");

		return ret;
}

template <typename ValidatorFunc>
struct validator{
    std::regex reg; //regex ktory bedzie uzyty do walidacji samego formatu
    ValidatorFunc isValid; //callable uzywany do ewentualnej pozniejszej walidacji, jesli trzeba zwalidowac wiecej niz tylko format, jesli wystarczy zwalidowac sam format, to callable bedzie po prostu zwracac true. Najchetniej dalbym tutaj jakis default type, zeby przy casie niepodania konkretnego callable taki wlasnie byl defaultowy, ale szczerze nie do konca wiem jak
};

bool validate(const std::string& attr){
    //chce miec mape, ktora mi zbounduje nazwa atrybutu -> validator(w postaci regex, funkcja walidujacaa). Walidatory zawsze sa wywolywane z std::smatch i ewentualnie dodatkowymi danymi);
    //Mam kilka typow walidatorow:
    // 1. Taki, ktory tylko weryfikuje, czy to co zostalo zcaptuerowane w grupie (liczba) jest w danym, specyficznym dla atrybutu przedziale
    // 2. Taki, ktory sprawdza to samo co u gory, ale dodatkowo to jaki jest przedzial zalezy od innej capture grupy atrybutu (centymetry, cale)
    // 3. Z uwagi, ze nie wiem jak zrobic default validator, ktory by zwracal true nie wazne co mu sie poda (probowalem setowac isValid na default value, ktorym byla funkcja przyjmujaca parameter pack i go ignorujaca, cos takiego:
    //     template <typename... Ts>
    //     constexpr bool retTrue(Ts... ts){return true;}
    //     ale mialem blad, ze nie w validatorze nie ma takiego konstruktora)
    //
    // jest jeszcze default validator, czyli wlasnie taki zwracajacy tylko true:
    auto firstValidator = [](const std::smatch& match, unsigned low, unsigned high){
        auto num = toUnsigned(match[1]);

        return low <= num && num <= high;
    };
    auto secondValidator = [](const std::smatch& match){
        auto num = toUnsigned(match[1]);

        if("cm" == match[2]) return 1 <= num && num <= 10;
        else return 10 <= num && num <= 20;
    };
    auto defaultValidator = [](auto ignore){return true;};
    //
    // wiec uzywam std::variant
    //aliasy, zeby moc sobie to rozdzielic ladnie na linijki i nie robic dowalonego onelinera
    using firstValidator_t = decltype(std::bind(firstValidator, 0, 0)); //chce miec ujednolicony sposob wywolywania funkcji, czyli za kazdym razem po prostu costam(smatch), wiec pomyslalem, ze uzyje std::bind, aby utworzyc obiekt, kory bedzie juz mial zindowane oba argumenty, a ja mu podam tylko smatch
    using secondValidator_t = decltype(secondValidator);
    using defaultValidator_t = decltype(defaultValidator);

    using validator_v = std::variant<validator<firstValidator_t>, validator<secondValidator_t>, validator<defaultValidator_t>>;

    // wiec tworze mape nazwa atrybutu -> variant z validatorami
    // niestety musze dawac wiele rzeczy explicit, bo kompilator nie chce mi ich wydedukowac co tylko zmniejsza czytelnosc kodu
    std::unordered_map<std::string, validator_v> validators{
        {"attr1", validator<firstValidator_t>{std::regex{"attr1:(\\d{1, 2})"}, std::bind(firstValidator, 1, 10)}}, // chce utworzyc obiekt, ktory po wywloaniu costam(smatch) zrobi tak naprawde firstValidator(smatch, 1, 10);
        {"attr2", validator<secondValidator_t>{std::regex{"attr2:(\\d{1,2})(b|a)"}, secondValidator}},
        {"attr3", validator<defaultValidator_t>{std::regex{"attr3:#([0-9a-f]{6}"}, defaultValidator}}, // najlepiej gdyby sie po prostu dalo validator{std::regex{"regex"}}
    };

    auto attrName = attr.substr(0, 3); //pobieram nazwe atrybutu, poniewaz atrybut jest w formacie 3 male litery:specyficzne rzeczy dla atrybutu np. abc:123, asd:#a1c1, xyz:12cm
    auto returnVisitor = [](const auto& validator){return validator;}; // tworze visitora, ktory po prostu zwraca mi to, co dostal

    auto reg = std::visit(returnVisitor, validators[attrName]).reg; // pobieram regex zboundowany do atrybutu
    auto vtorFunc = std::visit(returnVisitor, validators[attrName]).isValid; // pobieram validator zbudnowany do atrybutu

    std::smatch match;
    if(std::regex_match(attr, match, reg)){ // jesli atrybut ma poprawny format dodaktowo validuje go funkcja
        if(vtorFunc(match)) return true; // jesli funkcja powie, ze atrybut ma poprawny format + to co jest w capture group sie zgadza, zwracam true (ew jessli to defaultValidator, to po prostu if(true) return true
        return false; // w przeciwnym wypadku false
    }else return false;
}

int main(){
    /*
        zalozmy, ze to sa constraints dla atrybutow:
        attr1:
            attr1:[liczba], liczba musi byc z przedzialu 1-10
        attr2:
            attr2:[liczba]cm - jesli cm, to liczba musi byc z przedzialu 1-10
            attr2:[liczba]in - jesli in, to liczba musi byc z przedzialu 10-20
        attr3:
            attr3:#[mieszanka] - po hashu nastepuje mieszanka, czyli 6 cyfr 0-9 lub a-f dowolnie zmieszanych np. attr3:#123456, attr3:#abcdef, attr:#1a2b3c
    */

    std::vector<std::string> attributes{"attr1:1", "attr1:123", "attr2:10cm", "attr2:12cm", "attr2:15in", "attr2:120in", "attr3:#abc123", "attr3:#11111111111111"};

    for(auto& attr : attributes){
        std::cout<<attr<<" is "<<(validate(attr) ? "valid" : "invalid")<<'\n';
    }
}

W skrocie chyba chodzi mi o to, żeby móc mieć validatory tak, żeby móc je wywoływać tak samo, wyjaśniłem to myślę dość przejrzyście w tym kodzie i komentarzach

0

Okej, masz tu pomieszanie z poplątaniem. To w std::visit powinieneś wykonywać kod wyspecjalizowany dla danej klasy, a wartość z niego zwracana musi mieć wspólny typ - u ciebie tak nie jest, każda specjalizacja validator to osobny typ. Zamiast zwracać funkcję, wywołaj ją w visitorze.

0

Przykład poprawnego użycia variantu:

template<typename... Ts>
struct visitor : Ts... {
    using Ts::operator()...;
};

auto main() -> int
{
    using data = variant<int, double, string>;

    auto validate_int = [](int n){ return n == 42; };
    auto validate_double = [](double n){ return n == 0; };
    auto validate_string = [](string const& s){ return s.size() > 4; };

    using vis_t = visitor<
        decltype(validate_int),
        decltype(validate_double),
        decltype(validate_string)
    >;

    DBG((visit(vis_t{}, data{42})));
    DBG((visit(vis_t{}, data{0})));
    DBG((visit(vis_t{}, data{0.})));
    DBG((visit(vis_t{}, data{42.})));
    DBG((visit(vis_t{}, data{"adsa"})));
    DBG((visit(vis_t{}, data{"adsas"})));
}

https://wandbox.org/permlink/Ua1JNEknQFqCh3kJ

0

Jak lambdy są proste to można też użyć generic lambdy:
https://isocpp.org/wiki/faq/cpp14-language#generic-lambdas

Fajnie działa z visitorem tylko lambda się nieraz robi zbyt duża jak na lambde :P.

0
Czitels napisał(a):

Jak lambdy są proste to można też użyć generic lambdy:

https://isocpp.org/wiki/faq/cpp14-language#generic-lambdas

Fajnie działa z visitorem tylko lambda się nieraz robi zbyt duża jak na lambde :P.

A kto ci zabrania zamiast dłuższych lambd zrobić normalne funkcje?

0

@kq: ale wartość funkcji zwróconej zależy od tego co będzie w match_result (match_result w postaci std::smatch jest w każdej funkcji argumentem), które jest uzyskiwane poprzez walidację regexem z mapy.

0

Masz coś źle z mentalnym modelem. Część specyficzna dla danego typu powinna zostać wykonana w visitorze. Spójrz na mój przykład i zaadaptuj go dla swoich typów.

1 użytkowników online, w tym zalogowanych: 0, gości: 1