0

In the example below I can effectively strip the const, volatile and reference qualifiers and use the single specialization for shared pointers. This is solved by the adding one more level of abstraction. How could I solve this without doing so? I could I just use the specialisations and match on shared_pointer, shared_pointer const etc?

#include <iostream>
#include <type_traits>

namespace detail {
    template<typename T>
    struct display;

    template<typename T>
    struct display<std::shared_ptr<T>> {
        static void apply() {
            std::cout << __FUNCTION__ << std::endl;
        }
    };
}

template<typename T>
void display() {
    detail::display<std::remove_cvref_t<T>>::apply();
}

int main() {

    std::shared_ptr<int> t;
    display<decltype(t)>();

    return 0;
}

So I have come up with a solution which I like much better which I thought I would share.

template<typename T>
struct is_shared_pointer : std::false_type { };

template<template<typename > typename T, typename U>
struct is_shared_pointer<T<U>> : std::is_same<std::decay_t<T<U>>, std::shared_ptr<U>> {};

template<typename T, typename Enable = void>
struct display;

template<typename T>
struct display<T, std::enable_if_t<is_shared_pointer<T>::value>> {
    static void apply() {
        std::cout << "shared ptr: " << __FUNCTION__ << std::endl;
    }
};

template<typename T>
struct display<T, std::enable_if_t<std::is_integral_v<T>>> {
    static void apply() {
        std::cout << "integral :" << __FUNCTION__ << std::endl;
    }
};

template<typename T>
struct display<T, std::enable_if_t<std::is_void_v<T>>> {
    static void apply() {
        std::cout << "void: " << __FUNCTION__ << std::endl;
    }
};

template<typename T>
struct display<T, std::enable_if_t<std::is_floating_point_v<T>>> {
    static void apply() {
        std::cout << "floating: " << __FUNCTION__ << std::endl;
    }
};


int main() {

    std::shared_ptr<int> t;
    display<decltype(t)>();

    return 0;
}

That being said, I am open to suggestions, ideas and techniques.

Blair Davidson
  • 901
  • 12
  • 35
  • Sorry I could not understand what do you want , But I think you want this: `template struct RawMaker { using type = typename std::remove_reference< typename std::decay::type>::type; };` – H.M Feb 09 '23 at 09:32

0 Answers0