I am trying to write a parser. I have the parser class below, but I am struggling to write a generalized ParseMany function with templates. В настоящее время, чтобы разыграть, я использую parser.parse () , который возвращает необязательный с {} для ошибки анализов или TypetoParse при успешном. I was wondering if it was possible to generalize the ParseMany function for a variable number of types. I am using visual studio 2022, C++ 20, and my project compiles and runs without errors.
class Parser {
Lexer& lexer;
public:
template
std::optional Parse();
Parser(Lexer& lexer);
template
inline std::optional ParseMany() {
std::optional maybe_t1 = Parse();
if (!maybe_t1.has_value()) return {};
return ReturnType{ std::forward(maybe_t1.value()) };
}
template
inline std::optional ParseMany() {
std::optional maybe_t1 = Parse();
if (!maybe_t1.has_value()) return {};
std::optional maybe_t2 = Parse();
if (!maybe_t2.has_value()) return {};
return ReturnType{ std::forward(maybe_t1.value()), std::forward(maybe_t2.value()) };
}
template
inline std::optional ParseMany() {
std::optional maybe_t1 = Parse();
if (!maybe_t1.has_value()) return {};
std::optional maybe_t2 = Parse();
if (!maybe_t2.has_value()) return {};
std::optional maybe_t3 = Parse();
if (!maybe_t3.has_value()) return {};
return ReturnType{ std::forward(maybe_t1.value()), std::forward(maybe_t2.value()), std::forward(maybe_t3.value()) };
}
};
Подробнее здесь: https://stackoverflow.com/questions/795 ... c-template
Как обобщать эту функцию с помощью вариационного шаблона ⇐ C++
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение