Template function for handling class and class *

The code below allows me to create a function template by taking a parameter, which is a vector of one of three different types of pointers for Box objects:

const std::vector<std::shared_ptr<Box>>&
const std::vector<std::weak_ptr<Box>>&
const std::vector<Box*>&

Is there a way to expand this to support:

const vector<Box>& 
const vector<std::reference_wrapper<Box>>

maybe something in boost?

#include <vector>
#include <iostream>

class Box{
public:

    Box (unsigned int id, unsigned int side): id(id), side(side){}

    int volume(){
        return side * side * side;
    }
    unsigned int id;
    unsigned int side;

};

template <typename T>
struct is_box_containter {
    enum { value = false };
};

template <>
struct is_box_containter <std::vector<std::shared_ptr<Box>>> {
    enum { value = true };
};

template <>
struct is_box_containter <std::vector<std::weak_ptr<Box>>> {
    enum { value = true };
};

template <>
struct is_box_containter <std::vector<Box*>> {
    enum { value = true };
};

template <typename T>
typename std::enable_if<is_box_containter<T>::value>::type
measure(T const& boxes )
{
    for (auto& box : boxes) {
        std::cout << box->id << " has volume " << box->volume() << std::endl;
    }
}

int main (){

    std::vector<std::shared_ptr<Box>>  some_boxes;
    some_boxes.push_back(std::shared_ptr<Box>(new Box(1,4)));
    some_boxes.emplace_back(new Box(2, 12));
    Box * box_3 = new Box(3, 8);
    Box * box_4 = new Box(4, 9);

    std::vector<Box*>  more_boxes;
    more_boxes.emplace_back(box_3);
    more_boxes.emplace_back(box_4);

    measure(some_boxes);
    measure(more_boxes);

    return 0;
}

Why I ask this question: I have an application with two functions that implement almost identical logic. One takes a list of SomeClass, the other takes a vector of pointers to SomeClass. I am currently planning a code refactoring to replace the SomeClass list with a list of generic pointers to SomeClass. But the only reason I do this is to translate the logic into a common implementation. I do not want to do this if there is a perfectly reasonable way to avoid this.

+4
1

, , :

template<typename T> 
T& dereference(T &v) {
  return v;
}

template<typename T> 
const T& dereference(const T& v) {
  return v;
}

template<typename T> 
typename std::enable_if<!std::is_function<T>::value, T&>::type dereference(T* v) {
  return dereference(*v);
}

template<typename T> 
const T& dereference(const std::shared_ptr<T>& v) {
  return dereference(*v);
}

template<typename T> 
const T& dereference(const std::weak_ptr<T>& v) {
  return dereference(*v);
}

template<typename T> 
const T& dereference(const std::reference_wrapper<T>& v) {
  return v;
}

:

template <typename T>
typename std::enable_if<is_box_containter<T>::value>::type
measure(T const& boxes )
{
    for (auto& box : boxes) {
        std::cout << dereference(box).id 
                  << " has volume " << dereference(box).volume() << std::endl;
    }
}

LIVE DEMO

P.S :

template <>
struct is_box_containter <std::vector<Box>> {
    enum { value = true };
};

template <>
struct is_box_containter <std::vector<std::reference_wrapper<Box>>> {
    enum { value = true };
};
+4

Source: https://habr.com/ru/post/1610565/


All Articles