Evgenii Legotckoi
Feb. 26, 2019, 4:22 p.m.

Using SFINAE to specialize template methods, depending on the type of container std::map or std::vector, passed as an argument

There was a problem: "write the second implementation of the template function", in which the same arguments are passed. That is, the signature of both functions completely coincides. The difference is that, as an argument, the std::vector containers and the std::map containers can be passed to the function. And depending on whether std::vector is or std::map, this or that implementation should be selected.

In this case, the SFINAE rule will be used, which states: If the final types of arguments cannot be calculated (to perform substitution of template parameters) of an overloaded template function, the compiler does not throw an error, but searches for another suitable overload. The error will be in three cases:

  • No suitable overload was found.
  • Several such overloads were found, and C ++ cannot decide which one to take.
  • The overload was found, it turned out to be a template, and an error occurred when instantiating the template.

Our case will be the second. There are two overload template functions.


Implementation

To determine the required overload, std::enable_if will be used, which is used just for such cases to specialize a particular function depending on the type of data that needs to be processed.

The standard library STD, as it turned out, does not have the functionality to determine whether the container is std::map or not. But on StackOverflow this issue has already been discussed. The code given there needs only to be slightly corrected, and correctly applied to write a specialization of functions depending on the type of container.

The essence of the code is to decide at the compilation stage whether the element of the std::pair container is and, based on this, to decide whether the container is std::map. If the container is std::map, then select one function; in other cases, select the second implementation of the function.

  1. #include <iterator>
  2. #include <type_traits>
  3.  
  4. // templates for determining whether the std::pair container element is
  5. template <typename T>
  6. struct is_pair : std::false_type {};
  7.  
  8. template <typename T, typename U>
  9. struct is_pair<std::pair<T, U>> : std::true_type {};
  10.  
  11. template <typename T>
  12. constexpr bool is_pair_v = is_pair<T>::value;
  13.  
  14. // templates for determining if the container is std::map
  15. template<typename, typename = void>
  16. struct is_mapping : std::false_type {};
  17.  
  18. template <typename Container>
  19. struct is_mapping<Container, std::enable_if_t<is_pair_v<typename std::iterator_traits<typename Container::iterator>::value_type>>> : std::true_type {};
  20.  
  21. template <typename T>
  22. constexpr bool is_mapping_v = is_mapping<T>::value;
  23.  
  24. #include <map>
  25. #include <vector>
  26. #include <iostream>
  27.  
  28. class ClassWithSpecializedMethods
  29. {
  30. public:
  31. ClassWithSpecializedMethods() {}
  32.  
  33. // A specialized method for handling std::map
  34. template <class ContainerType>
  35. static typename std::enable_if<is_mapping_v<ContainerType>, void>::type printContainer(ContainerType& container);
  36.  
  37. // A specialized method for handling std::vector
  38. template <class ContainerType>
  39. static typename std::enable_if<!is_mapping_v<ContainerType>, void>::type printContainer(ContainerType& container);
  40. };
  41.  
  42. // Method implementations
  43. template <class ContainerType>
  44. inline
  45. static typename std::enable_if<is_mapping_v<ContainerType>, void>::type ClassWithSpecializedMethods::printContainer(ContainerType& container)
  46. {
  47. std::cout << "Map:" << std::endl;
  48. for (const auto& [key, value] : container)
  49. {
  50. std::cout << "Key: " << key << " Value: " << value << std::endl;
  51. }
  52. std::cout << std::endl;
  53. }
  54.  
  55. template <class ContainerType>
  56. inline
  57. static typename std::enable_if<!is_mapping_v<ContainerType>, void>::type ClassWithSpecializedMethods::printContainer(ContainerType& container)
  58. {
  59. std::cout << "Vector:" << std::endl;
  60. for (const auto& value : container)
  61. {
  62. std::cout << "Value: " << value << std::endl;
  63. }
  64. std::cout << std::endl;
  65. }
  66.  
  67. // Testing functions
  68. int main() {
  69. std::cout << "is_pair:" << std::endl;
  70. std::cout << "Map: " << is_pair_v<std::iterator_traits<std::map<int, int>::iterator>::value_type> << std::endl;
  71. std::cout << "Vector: " << is_pair_v<std::iterator_traits<std::vector<int>::iterator>::value_type> << std::endl;
  72. std::cout << std::endl;
  73. std::cout << "is_mapping:" << std::endl;
  74. std::cout << "Map: " << is_mapping_v<std::map<int, int>> << std::endl;
  75. std::cout << "Vector: " << is_mapping_v<std::vector<int>> << std::endl;
  76. std::cout << std::endl;
  77.  
  78. std::map<int, int> map_container = {{1, 1}, {2, 2}, {3, 3}};
  79. std::vector<int> vector_container = {1, 2, 3};
  80.  
  81. ClassWithSpecializedMethods::printContainer(map_container);
  82. ClassWithSpecializedMethods::printContainer(vector_container);
  83. }
  84.  

C++ standards used

For this code to work, you must use the C++17 standard; on a lesser standard, the code will not compile.

In QMake projects, the following lines should be added to the pro file

  1. CONFIG += console c++17
  2. QMAKE_CXXFLA
  3.  
  4. ## Conclusion
  5.  
  6. As a conclusion, we get the following conclusion
  7.  
  8.  
  9. ```lang-bsh
  10. is_pair:
  11. Map: 1
  12. Vector: 0
  13.  
  14. is_mapping:
  15. Map: 1
  16. Vector: 0
  17.  
  18. Map:
  19. Key: 1 Value: 1
  20. Key: 2 Value: 2
  21. Key: 3 Value: 3
  22.  
  23. Vector:
  24. Value: 1
  25. Value: 2
  26. Value: 3

What it looks like in the console

Вывод с использование SFINAE для std::map и std::vector

Do you like it? Share on social networks!

Comments

Only authorized users can post comments.
Please, Log in or Sign up
  • Last comments
  • Evgenii Legotckoi
    March 9, 2025, 9:02 p.m.
    К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
  • VP
    March 9, 2025, 4:14 p.m.
    Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
  • ИМ
    Nov. 22, 2024, 9:51 p.m.
    Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
  • Evgenii Legotckoi
    Oct. 31, 2024, 11:37 p.m.
    Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
  • A
    Oct. 19, 2024, 5:19 p.m.
    Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html