这个问题的灵感来自我之前的问题No template parameter deduction of parameter pack。
请考虑以下代码示例:
#include <memory>
#include <string>
template<typename... FArgs>
class Callback
{
public:
class Handle{};
};
class BaseCallbackHandle
{
};
using TypeErasedCallbackHandle = std::unique_ptr<BaseCallbackHandle>;
template<typename H>
TypeErasedCallbackHandle makeTypeErasedCallbackHandle( H handle)
{
return {};
}
int main()
{
Callback<int>::Handle h;
std::string s;
makeTypeErasedCallbackHandle(h); //should compile fine
makeTypeErasedCallbackHandle(s); //should raise a compile error
}
另见http://coliru.stacked-crooked.com/a/5f2a2e816eef6afd
函数模板makeTypeErasedCallbackHandle
现在将任何类作为输入参数。有没有办法确保(例如使用static-assert或enable_if),只允许Callback<FArgs...>::Handle
(任何FArgs
)作为H
? Callback<int>::Handle
的示例应该编译,而std::string
将失败。
答案 0 :(得分:1)
一种方法是通过传递一些额外的参数:
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
}
这使用identity trick
(Stephan T. Lavavej)来进行类型演绎。
答案 1 :(得分:1)
在Handle
类中定义类型,并在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
}
在实例化期间,对于没有定义嵌套类型的任何H
,这将失败。
通过更多的努力,您可以static_assert
向客户端生成有意义的消息,同时通过类型特征增加解决方案的灵活性。这样做的好处是callback_impl::is_callback
可以专门用于任意句柄类型:
#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;
}
输出:
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,
^~~~~~~~~~~~~
答案 2 :(得分:0)
您可以为您的班级成员制作虚构成员,并在替代方面将其称为: http://coliru.stacked-crooked.com/a/d5738766fd7ac45f
class Handle
{
public:
static void doNothing(){}
};
...
TypeErasedCallbackHandle makeTypeErasedCallbackHandle( H handle)
{
H::doNothing();
return {};
更标准的方法是使用类特征或静态断言,来自像BOOST这样的库(可能其中一些已经是语言的一部分)。
编辑: http://coliru.stacked-crooked.com/a/2a3adcb9d9dd274c 这种方式稍好一些,var而不是call。
答案 3 :(得分:0)
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 {};
}
此应有效,只允许您在回调中使用该功能