附加到可变参数 C 宏中的每个参数
Appending to every arg in variadic C macro
我正在尝试创建一个类似于高级语言中的 "where" 约束的宏:
#define where(T, ...) typename std::enable_if< /* tricky part */ >::type
// should expand like so: trait1<T>::value && trait2<T>::value ... traitN<T>::value
所以 VA_ARGS 是特征列表并且像这样使用:
template<class T,
where(T, std::is_default_constructible, std::is_class)>
class A { };
这可能吗?
也许是这样的:
#define WHERE(P, ...) join_predicate<P, __VA_ARGS__>::value
template <template <typename> typename Pred, typename ...Args>
struct join_predicate : std::conjunction<Pred<Args>...> {};
用法:
std::enable_if_t<WHERE(std::is_default_constructible, Foo, Bar, Quz), int>
我可能甚至不会理会宏而只使用真正的 C++ 构造,也许:
template <template <typename> typename P, typename ...Args>
constexpr bool where_v = join_predicate<P, Args...>::value;
我很抱歉在四年后把这个问题从死里复活,但我认为我有一个更好的答案,这正是我当时的目标。 (不需要多次指定谓词的参数。)
template <typename T, template <typename> class... Predicates>
using where_t = std::enable_if_t<std::conjunction_v<Predicates<T>...>>;
template <typename T, typename = where_t<T, std::is_default_constructible, std::is_class>>
struct my_struct
{
};
我正在尝试创建一个类似于高级语言中的 "where" 约束的宏:
#define where(T, ...) typename std::enable_if< /* tricky part */ >::type
// should expand like so: trait1<T>::value && trait2<T>::value ... traitN<T>::value
所以 VA_ARGS 是特征列表并且像这样使用:
template<class T,
where(T, std::is_default_constructible, std::is_class)>
class A { };
这可能吗?
也许是这样的:
#define WHERE(P, ...) join_predicate<P, __VA_ARGS__>::value
template <template <typename> typename Pred, typename ...Args>
struct join_predicate : std::conjunction<Pred<Args>...> {};
用法:
std::enable_if_t<WHERE(std::is_default_constructible, Foo, Bar, Quz), int>
我可能甚至不会理会宏而只使用真正的 C++ 构造,也许:
template <template <typename> typename P, typename ...Args>
constexpr bool where_v = join_predicate<P, Args...>::value;
我很抱歉在四年后把这个问题从死里复活,但我认为我有一个更好的答案,这正是我当时的目标。 (不需要多次指定谓词的参数。)
template <typename T, template <typename> class... Predicates>
using where_t = std::enable_if_t<std::conjunction_v<Predicates<T>...>>;
template <typename T, typename = where_t<T, std::is_default_constructible, std::is_class>>
struct my_struct
{
};