繁体   English   中英

用于确定STL容器的模板参数上的std :: enable_if

[英]std::enable_if on a template argument to determine STL container

基于来自Nawaz的答案,我想使用enable_if来确定模板参数是否为容器,如果是,我想显示类型名称的自定义消息,而不是来自typeid的名称。 我已经通过两种方式实现了模板专业化。 代码可以编译并运行,但在任何情况下都不会调用专用方法。 我假设我错误地使用了enable_if,这里正确的应用是什么?

我在代码序列的下面放置了一个自包含的小控制台应用程序:(a)必需的包含文件(b)预备模板代码(使用SFINAE)(c)应该执行任务的结构的两个实现( d)一些客户代码

#include <typeinfo>
#include <string>
#include <list>
#include <vector>
#include <iostream>
using namespace std;

template<typename T>
struct has_const_iterator
{
private:
    typedef char                      yes;
    typedef struct { char array[2]; } no;

    template<typename C> static yes test(typename C::const_iterator*);
    template<typename C> static no  test(...);
public:
    static const bool value = sizeof(test<T>(0)) == sizeof(yes);
    typedef T type;
};

template <typename T>
struct has_begin_end
{
    template<typename C> static char(&f(typename std::enable_if<
        std::is_same<static_cast<typename C::const_iterator(C::*)() const>(&C::begin),
        typename C::const_iterator(C::*)() const>::value, void>::type*))[1];

    template<typename C> static char(&f(...))[2];

    template<typename C> static char(&g(typename std::enable_if<
        std::is_same<static_cast<typename C::const_iterator(C::*)() const>(&C::end),
        typename C::const_iterator(C::*)() const>::value, void>::type*))[1];

    template<typename C> static char(&g(...))[2];

    static bool const beg_value = sizeof(f<T>(0)) == 1;
    static bool const end_value = sizeof(g<T>(0)) == 1;
};
template<typename T>
struct is_container : std::integral_constant<bool,
    has_const_iterator<T>::value &&
    has_begin_end<T>::beg_value &&
    has_begin_end<T>::end_value>
{ };

struct TypeName {
    template <typename T>
    static const char* get() {
        return typeid(T).name();
    }

    template <typename T, typename std::enable_if<is_container<T>::value>::type >
    static const char* get()
    {
        typedef typename T::value_type ElementType;
        std:string containerType = "";
        if (std::is_same<decltype(std::vector<ElementType>), T>::value) {
            containerType = "(Vector) ";
        }
        if (std::is_same<decltype(std::list<ElementType>), T>::value) {
            containerType = "(List) ";
        }
        std::string returnString = "Container " + containerType;
        returnString += " of ";
        returnString += get<ElementType>();
        return returnString.c_str();
    }
};

template <typename T> struct GypeName {

    static const char* get() {
        return typeid(T).name();
    }

    template <class = typename std::enable_if<is_container<T>::value>::type >
    static const char* get()
    {
        typedef typename T::value_type ElementType;
        std:string containerType = "";
        if (std::is_same<decltype(std::vector<ElementType>), T>::value) {
            containerType = "(Vector) ";
        }
        if (std::is_same<decltype(std::list<ElementType>), T>::value) {
            containerType = "(List) ";
        }
        std::string returnString = "Container " + containerType;
        returnString += " of ";
        returnString += GypeName<ElementType>::get();
        return returnString.c_str();
    }
};



int main(int argc, char** argv) {
    cout << is_container<int>::value << endl;
    cout << is_container<std::vector<int>>::value << endl;
    cout << TypeName::get<int>() << endl;
    cout << TypeName::get<std::string>() << endl;
    cout << TypeName::get<std::vector<int>>() << endl;
    cout << TypeName::get<std::vector<std::vector<int>>>() << endl;
    cout << GypeName<int>::get() << endl;
    cout << GypeName<std::string>::get() << endl;
    cout << GypeName<std::vector<int>>::get() << endl;
    cout << GypeName<std::vector<std::vector<int>>>::get() << endl;
    return 0;
}

所有这些的输出是

0
1
int
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
class std::vector<int,class std::allocator<int> >
class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > >
int
class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >
class std::vector<int,class std::allocator<int> >
class std::vector<class std::vector<int,class std::allocator<int> >,class std::allocator<class std::vector<int,class std::allocator<int> > > >

在两种情况下都不调用专用功能。

您可以使用以下内容:

struct TypeName {
    template <typename T>
    static
    std::enable_if_t<!is_container<T>::value, const char*>
    get() {
        return typeid(T).name();
    }

    template <typename T>
    static
    std::enable_if_t<is_container<T>::value, std::string>
    get()
    {
        typedef typename T::value_type ElementType;
        std::string containerType = "";
        if (std::is_same<std::vector<ElementType>, T>::value) {
            containerType = "(Vector) ";
        }
        if (std::is_same<std::list<ElementType>, T>::value) {
            containerType = "(List) ";
        }
        return (boost::format("Container %s of %s")
                % containerType
                % TypeName::get<ElementType>()).str();
    }
};

演示版

请注意, std::string被视为char的容器。
由于向量/列表具有特定的(运行时:()情况,因此您可以仅对这两个使用特化:

namespace detail
{
    template <typename T> struct TypeName
    {
        auto operator ()() const { return typeid(T).name(); }  
    };

    template <template <typename...>class C,  typename T, typename...Ts>
    struct TypeName<C<T, Ts...>>
    {
        auto operator()() const {
            return (boost::format("container of %s") % TypeName<T>{}()).str();
        }
    };

    template <typename T, typename A>
    struct TypeName<std::vector<T, A>>
    {
        auto operator()() const {
            return (boost::format("Vector of %s") % TypeName<T>{}()).str();
        }
    };

    template <typename T, typename A>
    struct TypeName<std::list<T, A>>
    {
        auto operator()() const {
            return (boost::format("List of %s") % TypeName<T>{}()).str();
        }
    };

}

struct TypeName {
    template <typename T>
    static auto get() {
        return detail::template TypeName<T>{}();
    }
};

演示版

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM