Is it possible to specialize a template definition based on the existence of a nested typedef of a template type parameter?

前端 未结 3 1189
一整个雨季
一整个雨季 2021-02-10 02:57

I have a template, template class wrapper, that I would like to specialize based on the existence of typename T::context_type. If

3条回答
  •  无人及你
    2021-02-10 03:16

    Using @K-ballo's answer, I wrote the following:

    namespace detail {
    BOOST_MPL_HAS_XXX_TRAIT_DEF(context_type)
    }
    
    template 
    class wrapper
    {
    public:
        wrapper() {
            std::cout << "T::context_type does not exist." << std::endl;
        }
    };
    
    template 
    class wrapper >::type>
    {
    public:
        typedef typename T::context_type context_type;
    
    private:
        context_type ctx;
    
    public:
        wrapper(context_type ctx_)
            : ctx(ctx_)
        {
            std::cout << "T::context_type exists." << std::endl;
        }
    };
    

    Now, the sample code compiles and outputs:

    T::context_type exists.
    T::context_type does not exist.
    

提交回复
热议问题