C/C++ 通过模板来推导 “std::shared_ptr<T>”;T泛型成员类型

C/C++ 11 语言标准库提供的 std::remove_pointer<T>,是没有办法删除 “共享引用指针” 获取其T泛型成员类型的,但人们可以自行实现相应的功能。

C/C++ 11 语言上面,可以通过 “decltype(expression)” 关键字来让编译器推导并定义类型,但对于 C/C++ 工程师而言,好的办法是使用 “泛型模板” 来推导并定义其成员类型。

参考实现:

泛型:frp/stdafx.h at main · liulilittle/frp (github.com)

应用: frp/SslSocket.hpp at main · liulilittle/frp (github.com)

模板应用:

namespace frp {
    namespace transmission {
        namespace templates {
            template<class T>
            class SslSocket : public IDisposable {
                inline bool                                         HandshakeAsync(HandshakeType type, const BOOST_ASIO_MOVE_ARG(HandshakeAsyncCallback) callback) noexcept {
                    typedef typename stl::remove_pointer<T>::type SslSocket; /* decltype(*ssl_socket_); */
                }
            };
        }
    }
}

模板实现:

namespace stl {
    template<class T>
    struct remove_pointer {
        typedef T type;
    };

    template<class T>
    struct remove_pointer<T*> {
        typedef T type;
    };

    template<class T>
    struct remove_pointer<T* const> {
        typedef T type;
    };

    template<class T>
    struct remove_pointer<T* volatile> {
        typedef T type;
    };

    template<class T>
    struct remove_pointer<T* const volatile> {
        typedef T type;
    };

    template<class T>
    struct remove_pointer<std::shared_ptr<T> > {
        typedef T type;
    };

    template<class T>
    struct remove_pointer<std::shared_ptr<T>&> {
        typedef T type;
    };

    template<class T>
    struct remove_pointer<std::shared_ptr<T> const&> {
        typedef T type;
    };
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值