У меня есть концепция SmartPointer, поэтому я могу иметь функции который может принимать либо std::unique_ptr, либо std::shared_ptr:
Код: Выделить всё
template
concept SmartPointer = requires(const T& t) {
requires std::same_as;
};
Я могу создать функцию, которая имеет параметр шаблона с ограничением SmartPointer, и проверить это:
Код: Выделить всё
template
requires std::same_as
void doWithSmartPointers(TIterator begin, TIteratorSentinel end) {
for (auto it = begin; it != end; ++it) {
// Some logic with it->get() etc.
}
}
Код: Выделить всё
std::vector v{};
v.push_back(std::make_unique(1));
v.push_back(std::make_unique(2));
v.push_back(std::make_unique(3));
doWithSmartPointer(v.begin(), v.end()); // Error, couldn't infer template argument T
doWithSmartPoint(v.begin(), v.end()); // OK
p>
По сути, мне нужно что-то вроде этого:
Код: Выделить всё
template
requires std::same_as // Not valid syntax!
void doWithSmartPointers(TIterator begin, TIteratorSentinel end) {
for (auto it = begin; it != end; ++it) {
// Some logic with it->get() etc.
}
}
Подробнее здесь: https://stackoverflow.com/questions/699 ... er-concept
Мобильная версия