I am trying to write a program that sort only numbers in a std::vector or a std::list for which I did 2 concepts:
template<typename T>
concept ValidContainer = requires(T a) {
std::same_as<T, std::vector<typename T::value_type>>;
std::same_as<T, std::list<typename T::value_type>>;
};
And:
template<typename T>
concept Sortable = requires(T a) {
ValidContainer<T> && std::same_as<typename T::value_type, int>;
ValidContainer<T> && std::same_as<typename T::value_type, float>;
};
And the signature of the function that sorts them:
void BubbleSort(Sortable auto& collection)
{
// sort algorithm
}
The question that arises in my head is when I pass a std::vector<std::string> it does not show me a compilation error?
int main()
{
std::vector<int> test = { 32, 3, 6, 8, 2, 5, 0, 43, 67, 1 };
std::vector<std::string> test2 = { "first", "second", "third" };
BubbleSort(test); // this is ok
BubbleSort(test2); // shouldn't this be a compile error?
return 0;
}