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

SFINAE, C++17, template

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.

#include <iterator>
#include <type_traits>

// templates for determining whether the std::pair container element is
template <typename T>
struct is_pair : std::false_type {};

template <typename T, typename U>
struct is_pair<std::pair<T, U>> : std::true_type {};

template <typename T>
constexpr bool is_pair_v = is_pair<T>::value;

// templates for determining if the container is std::map
template<typename, typename = void>
struct is_mapping : std::false_type {};

template <typename Container>
struct is_mapping<Container, std::enable_if_t<is_pair_v<typename  std::iterator_traits<typename Container::iterator>::value_type>>> : std::true_type {};

template <typename T>
constexpr bool is_mapping_v = is_mapping<T>::value;

#include <map>
#include <vector>
#include <iostream>

class ClassWithSpecializedMethods
{
public:
    ClassWithSpecializedMethods() {}

    // A specialized method for handling std::map
    template <class ContainerType>
    static typename std::enable_if<is_mapping_v<ContainerType>, void>::type printContainer(ContainerType& container);

    // A specialized method for handling std::vector
    template <class ContainerType>
    static typename std::enable_if<!is_mapping_v<ContainerType>, void>::type printContainer(ContainerType& container);
};

// Method implementations
template <class ContainerType>
inline
static typename std::enable_if<is_mapping_v<ContainerType>, void>::type ClassWithSpecializedMethods::printContainer(ContainerType& container)
{
    std::cout << "Map:" << std::endl;
    for (const auto& [key, value] : container)
    {
        std::cout << "Key: " << key << " Value: " << value << std::endl;
    }
    std::cout << std::endl;
}

template <class ContainerType>
inline
static typename std::enable_if<!is_mapping_v<ContainerType>, void>::type ClassWithSpecializedMethods::printContainer(ContainerType& container)
{
    std::cout << "Vector:" << std::endl;
    for (const auto& value : container)
    {
        std::cout << "Value: " << value << std::endl;
    }
    std::cout << std::endl;
}

// Testing functions
int main() {
    std::cout << "is_pair:" << std::endl;
    std::cout << "Map:    " << is_pair_v<std::iterator_traits<std::map<int, int>::iterator>::value_type> << std::endl;
    std::cout << "Vector: " << is_pair_v<std::iterator_traits<std::vector<int>::iterator>::value_type>   << std::endl;
    std::cout << std::endl;
    std::cout << "is_mapping:" << std::endl;
    std::cout << "Map:    " << is_mapping_v<std::map<int, int>> << std::endl;
    std::cout << "Vector: " << is_mapping_v<std::vector<int>>   << std::endl;
    std::cout << std::endl;

    std::map<int, int> map_container = {{1, 1}, {2, 2}, {3, 3}};
    std::vector<int> vector_container = {1, 2, 3};

    ClassWithSpecializedMethods::printContainer(map_container);
    ClassWithSpecializedMethods::printContainer(vector_container);
}

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

CONFIG += console c++17
QMAKE_CXXFLA

## Conclusion

As a conclusion, we get the following conclusion


```lang-bsh
is_pair:
Map:    1
Vector: 0

is_mapping:
Map:    1
Vector: 0

Map:
Key: 1 Value: 1
Key: 2 Value: 2
Key: 3 Value: 3

Vector:
Value: 1
Value: 2
Value: 3

What it looks like in the console

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

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.
Support the author Donate

Comments

Only authorized users can post comments.
Please, Log in or Sign up
Looking for a Job?
25,000.00 руб. - 30,000.00 руб.
Разработчик Qt/C++
Barnaul, Altai Krai, Russia

For registered users on the site there is a minimum amount of advertising

DSh
Sept. 21, 2019, 2:55 p.m.
Dmitrij Shilkov

C++ - Test 001. The first program and data types

  • Result:46points,
  • Rating points-6
s
Sept. 18, 2019, 5:19 p.m.
sanyalitv

C++ - Test 002. Constants

  • Result:33points,
  • Rating points-10
s
Sept. 18, 2019, 5:12 p.m.
sanyalitv

C++ - Test 001. The first program and data types

  • Result:80points,
  • Rating points4
Last comments
M
Sept. 20, 2019, 11:25 a.m.
Mark

вызываю метод get у m_downloader в другом методе и приложение начинает вылетать. В чем ошибка?
M
Sept. 19, 2019, 5:45 a.m.
Mark

А вот как выгрузить файл на сервер по http протоколу? Допустим на regRu. И как получить путь файла, которой отображается в файловом менеджере regRu, чтобы загрузить его.
Sept. 17, 2019, 6:07 a.m.
Misha Lebedev

Кстати интересные темы нашёл тут https://emacsway.github.io/ru/django-framework/#django-models Может что полезного тоже Евгений найдёте
Sept. 17, 2019, 4:50 a.m.
Misha Lebedev

Доброго времени суток. Спасибо за хороший ответ, У меня ситуация така что в галлереи будет несколько миллионов фотографий с фильтрами и тегами , и я опасаюсь за производительност . Это ос…
Sept. 17, 2019, 3:23 a.m.
Evgenij Legotskoj

Добрый день. Да, я тоже читал ту статью в своё время и согласен с тем, что внешние ключи гораздо лучше, чем GenericForeignKey. Выборки в ряде случае работают быстрее. Но лично мне про…
Now discuss on the forum
M
Sept. 22, 2019, 1:54 a.m.
Mark

Пока реализовал так: на сервере лежат сами файлы и файл с именами всех файлов и их версий, который админ обновляет при довавлении файла. Клиентское по парсит этот файл и таким образом понимает к…
MS
Sept. 21, 2019, 12:51 p.m.
Mihail Sermjazhko

Так работает import QtQuick 2.7import QtQuick.Window 2.2import QtQuick.Controls 2.0Window { id: demo width: 800 height: 600 visible: true color: "#ff303030" prope…
MS
Sept. 21, 2019, 11:46 a.m.
Mihail Sermjazhko

Говорят через делегат должно работать: ListView{ id:l anchors.fill: parent model: ["data 1","data 2","data 3"] currentIndex : 2 delegate: Text { Rectangle{ …
C
Sept. 21, 2019, 9:08 a.m.
Cobra91151

Здравствуйте! Я хочу подключатся к сетям WiFi Enterprise через свою программу. Настроил Radius Server, поключение к сети через Windows работает. Но при подключении через программу пишет: о…
Sept. 20, 2019, 4:56 a.m.
Pavel K.

Привет , подскажите кто-нибудь , как сделать драг н дроп , не нарушая при этом логику работы зума? import QtQuick 2.6 import QtGraphicalEffects 1.0 Page { id:win property string fi…
EVILEG
About
Services
© EVILEG 2015-2019
Recommend hosting TIMEWEB