어떤 이유인지 부스트의 변형을 사용하고 있습니다. (저는 C++ 17 버전이 있습니다. 여기에 이전 버전과의 호환성을 제공하고 있으며, C++ 17은 clang ++에서 완전히 지원하지 않습니다). 방문자 패러다임을 사용하여 :3 args를 지원하지 않는 변형 apply_visitor를 얻는 방법?
이 인수를 예상대로 13 출력
#include <iostream>
#include <boost/variant.hpp>
using boost::variant;
typedef boost::variant<double, std::string> term_t;
class plus_visitor : public boost::static_visitor<term_t> {
public:
term_t operator()(double lhs, double rhs) const{
return {lhs + rhs};
}
term_t operator()(double lhs, std::string rhs) const{
return {lhs + std::stoi(rhs)};
}
term_t operator()(std::string lhs, int rhs) const{
return operator()(rhs, lhs);
}
term_t operator()(std::string lhs, std::string rhs) const{
return std::stoi(lhs) + std::stoi(rhs);
}
};
int main(){
// term_t lhs {3.0};
term_t rhs {"10"};
term_t lhs {"3"};
term_t res = boost::apply_visitor(plus_visitor(), lhs, rhs);
std::cout << res << std::endl;
return 0;
}
작동합니다.
3 인수 거래는 여기에 무엇
#include <iostream>
#include <boost/variant.hpp>
using boost::variant;
typedef boost::variant<double, std::string> term_t;
class plus_visitor : public boost::static_visitor<term_t> {
public:
term_t operator()(double lhs, double rhs, double x) const{
return {lhs + rhs + x};
}
term_t operator()(double lhs, std::string rhs, double x) const{
return {lhs + std::stoi(rhs) + x};
}
term_t operator()(std::string lhs, double rhs, double x) const{
return operator()(rhs, lhs, x);
}
term_t operator()(std::string lhs, std::string rhs, double x) const{
return std::stoi(lhs) + std::stoi(rhs) + x;
}
};
int main(){
term_t rhs {"10"};
term_t lhs {"3"};
term_t x {3.0};
term_t res = boost::apply_visitor(plus_visitor(), lhs, rhs, x);
std::cout << res << std::endl;
return 0;
}
폭발? apply_visitor
은 2 개의 인수에 대해서만 작동합니까?
진단
나는 문서를보고, 그 발견 :
이진 인수 또는 단항 인수 http://www.boost.org/doc/libs/1_36_0/doc/html/boost/apply_visitor.html
apply_visitor
작품! 이거 끔찍해! 이 문제를 해결하고 구문에서 완전히 추악하지 않고 3 번째를 제공하려면 어떻게해야합니까? 더 많은 입력을 허용하는 다른 boost
라이브러리 함수가 있습니까? 부스트 라이브러리가 임의의 크기의 apply_visitor
함수를 허용하기 위해 가변 템플릿을 사용하지 않는 이유는 무엇입니까?
BTW, 3 번째 매개 변수가'std :: string' 인 것들을 잊어 버렸습니다. – Jarod42
@ Jarod42 알아요. 나는 여기서 한 점을 증명하려고 노력하고있다. – OneRaynyDay
증명 된 점. 부스트를 사용하려면 제한 사항을 처리해야합니다. 두 개 이상의 인수가 필요한 경우, 원하는만큼의 값을 포함하는 단일 구조로 대체하십시오. –