我正在尝试为同步/异步调用行为实现可编译时配置的回调。
这是第一种方法:
//emit type's
enum EEmitType
{
SYNC,
ASYNC,
};
//general
template<EEmitType et, typename... Args>
class callback_impl;
//implementation
template<EEmitType et, typename R, typename... Args>
class callback_impl<et, R(Args...)>
{ /*todo*/ };
//................................................................
//convert enum value to type or SFINAE
template <EEmitType et>
using callback_emit_type = std::integral_constant<EEmitType, et>;
//(for default SYNC)
template <typename T>
struct is_emit_type : std::false_type {};
//(for any other implementation)
template <EEmitType et>
struct is_emit_type<callback_emit_type<et>> : std::true_type {};
//................................................................
//metafunction
template <typename T>
using is_emit_type_t = typename is_emit_type<T>::type;
//................................................................
//for decl. like: callback<void()>
template <typename _unused, typename... Args>
struct construct_callback_impl
{
//alias on implementation
using type = callback_impl<SYNC, Args...>;
};
//for decl. like: callback<callback_emit_type<ASYNC>, void()>
template <typename EEmitType, typename... Args>
struct construct_callback_impl<typename
std::enable_if<is_emit_type_t<EEmitType>::value>::type, EEmitType, Args...>
{
//alias on implementation
using type = callback_impl<EEmitType::value, Args...>;
};
//................................................................
//user alias
template <typename... Args>
using callback = typename construct_callback_impl<Args...>::type;
现在使用:
callback<int(int)> ff_s; //<-- uses undefined class 'callback_impl<SYNC>'
callback<callback_emit_type<ASYNC>, int(int)> ff_a; //<--OK
当然,因为第一个args正在吃东西,并且为了成功编译,应该这样写:
callback<int(int), int(int)> ff_s
但是,这当然不是不可接受的。 好的,然后我尝试从Args中提取EEmitType ...
//for decl. like: callback<void()>
template <typename... Args>
struct construct_callback_impl
{
//alias on implementation
using type = int; //temporary stub
};
//for decl. like: callback<callback_emit_type<ASYNC>, void()>
template <typename... Args>
struct construct_callback_impl<typename std::enable_if<is_emit_type_t< typename std::tuple_element_t<0, std::tuple<Args...> >::type >::value>::type, Args...>
{
//alias on implementation
using type = int; //temporary stub
};
现在我得到了:
error C2338: tuple index out of bounds
note: see reference to class template instantiation 'std::tuple_element<0,std::tuple<>>' being compiled
答案 0 :(得分:2)
类似的事情应该起作用
template <typename>
struct emit_type_value : std::integral_constant<EEmitType, SYNC> {};
template <EEmitType x>
struct emit_type_value<callback_emit_type<x>> : std::integral_constant<EEmitType, x> {};
//for decl. like: callback<void()>
template <typename Arg0, typename ... Args>
struct construct_callback_impl
{
//alias on implementation
using type = std::conditional_t<is_emit_type<Arg0>::value,
callback_impl<emit_type_value<Arg0>::value, Args...>,
callback_impl<SYNC, Arg0, Args...>>;
};
如果您只使用false
和true
而不是SYNC
和ASYNC
,那会更麻烦。