Enable template only for specific templated class

自作多情 提交于 2019-12-05 06:38:28

Define a type within your Handle class, and refer to that type inside makeTypeErasedCallbackHandle():

#include <memory>
#include <string>

template <typename... FArgs>
struct Callback {
    struct Handle {
        using callback_type = Callback<FArgs...>;    
    };
};

struct BaseCallbackHandle {
};

using TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>;

template <typename H>
TypeErasedCallbackHandle makeTypeErasedCallbackHandle(H handle) {
    using callback_type = typename H::callback_type;

    return {};
}

int main() {
    Callback<int>::Handle h;
    std::string s;
    makeTypeErasedCallbackHandle(h); //should compile fine
    makeTypeErasedCallbackHandle(s); //should raise a compile error
}

Live example

This will fail during instantiation for any H that doesn't define the nested type.


With a little more effort, you can static_assert to produce a meaningful message to the client, while at the same time increasing the flexibility of the solution via type traits. This has the advantage that callback_impl::is_callback can be specialised for arbitrary handle types:

#include <memory>
#include <string>

namespace callback_impl {

struct callback_identification_type {};

template <typename T, typename = void>
struct is_callback : std::false_type {};

template <typename T>
struct is_callback<T,
    std::enable_if_t<std::is_same<typename T::callback_id_type,
                     callback_identification_type>::value>>
 : std::true_type {};

}

template <typename... FArgs>
struct Callback {
    struct Handle {
        using callback_id_type = callback_impl::callback_identification_type;    
    };
};

struct BaseCallbackHandle {
};

using TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>;

template <typename H>
TypeErasedCallbackHandle makeTypeErasedCallbackHandle(H handle) {
    static_assert(callback_impl::is_callback<H>::value,
                  "The handle type is not a member of a recognised Callback<T...>");
    return {};
}

int main() {
    Callback<int>::Handle h;
    std::string s;
    makeTypeErasedCallbackHandle(h); //should compile fine
    makeTypeErasedCallbackHandle(s); //should raise a compile error

    return 0;
}

Live example

Output:

g++ -std=c++14 -O2 -Wall -Wno-unused-local-typedefs -pedantic -pthread main.cpp && ./a.out
main.cpp: In instantiation of 'TypeErasedCallbackHandle makeTypeErasedCallbackHandle(H) [with H = std::__cxx11::basic_string<char>; TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>]':
main.cpp:41:35:   required from here
main.cpp:32:5: error: static assertion failed: The handle type is not a member of a recognised Callback<T...>
     static_assert(callback_impl::is_callback<H>::value,
     ^~~~~~~~~~~~~

One way to do it is by passing some extra arguments:

template <typename... Pack> struct packer {};

using TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>;

template <typename... T>
TypeErasedCallbackHandle makeTypeErasedCallbackHandle(typename Callback<T...>::Handle h, T...)
{
   return {};
}

template <typename... T>
TypeErasedCallbackHandle makeTypeErasedCallbackHandle_2(typename Callback<T...>::Handle h, packer<T...>)
{
   return {};
}

int main()
{
    Callback<int>::Handle h;
    std::string s;
    makeTypeErasedCallbackHandle(h, 0); //should compile fine
    // OR
    makeTypeErasedCallbackHandle_2(h, packer<int>());

    //makeTypeErasedCallbackHandle(s); //should raise a compile error
}

This makes use of identity trick (by Stephan T. Lavavej) for doing the type deduction.

You can make fictive member for your class family, and call it on substitution side: http://coliru.stacked-crooked.com/a/d5738766fd7ac45f

class Handle
{
    public:
    static void doNothing(){}
};

...

TypeErasedCallbackHandle makeTypeErasedCallbackHandle( H handle)
{
   H::doNothing();
   return {};

More standard way is to use class traits or static asserts, from libraries like BOOST (may be some of them are part of the language already).

EDIT: http://coliru.stacked-crooked.com/a/2a3adcb9d9dd274c This way is slightly better, var instead of call.

class BaseCallback
{
  ~BaseCallback() = 0;
};

template<typename... FArgs>
class Callback : public BaseCallback
{
  ~Callback(){...}
  ...
};

template<typename H>
TypeErasedCallbackHandle makeTypeErasedCallbackHandle( H handle)
{
  static_assert(std::is_base_of<BaseCallback, H>::value, "Must use a callback");
  return {};
}

This should work and only allow you to use that function on callbacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!