概念中的嵌套类型绑定在 GCC 和 clang 上失败但在 msvc 上失败
Nested type binding in concept fails on GCC and clang but not msvc
此代码:
template<typename T, template<typename> typename Pred>
concept sats_pred = static_cast<bool>(Pred<T>::value);
template<template<typename...> typename A, template<typename> typename Cond>
struct is_container_of_helper {
template<sats_pred<Cond>... Ts>
void operator()(const A<Ts...>&) const
{}
};
template<typename T>
struct always_true {
static constexpr bool value = true;
};
template<typename T, template<typename...> typename Container, template<typename> typename Cond>
concept is_container_of_if = requires(const T& v, const is_container_of_helper<Container, Cond>& h)
{
h(v);
};
template<typename T, template<typename...> typename A>
concept is = is_container_of_if<T, A, always_true>;
template<template<typename...> typename A>
struct is_a {
template<typename T>
struct type {
static constexpr bool value = is<T, A>;
};
};
template<typename T, template<typename...> typename Contained, template<typename...> typename Container>
concept is_container_of = is_container_of_if<T, Container, typename is_a<Contained>::type>;
不在 gcc 或 clang 主干下编译,但在 msvc 下编译 (godbolt)。在 gcc/clang 下它给出
expected a class template, got 'typename is_a<Contained>::type
此代码有效吗?如果没有,有没有一种方法可以用有效代码实现同样的事情,为什么 msvc 会编译它?
像这样更改代码的最后一部分:
template<typename T, template<typename...> typename Contained, template<typename...> typename Container>
concept is_container_of = is_container_of_if<T, Container, is_a<Contained>::template type>;
它在 clang 和 gcc 中编译。 is_container_of_if
需要一个 模板 作为最后一个模板参数,但您试图传递一个类型。
此代码:
template<typename T, template<typename> typename Pred>
concept sats_pred = static_cast<bool>(Pred<T>::value);
template<template<typename...> typename A, template<typename> typename Cond>
struct is_container_of_helper {
template<sats_pred<Cond>... Ts>
void operator()(const A<Ts...>&) const
{}
};
template<typename T>
struct always_true {
static constexpr bool value = true;
};
template<typename T, template<typename...> typename Container, template<typename> typename Cond>
concept is_container_of_if = requires(const T& v, const is_container_of_helper<Container, Cond>& h)
{
h(v);
};
template<typename T, template<typename...> typename A>
concept is = is_container_of_if<T, A, always_true>;
template<template<typename...> typename A>
struct is_a {
template<typename T>
struct type {
static constexpr bool value = is<T, A>;
};
};
template<typename T, template<typename...> typename Contained, template<typename...> typename Container>
concept is_container_of = is_container_of_if<T, Container, typename is_a<Contained>::type>;
不在 gcc 或 clang 主干下编译,但在 msvc 下编译 (godbolt)。在 gcc/clang 下它给出
expected a class template, got 'typename is_a<Contained>::type
此代码有效吗?如果没有,有没有一种方法可以用有效代码实现同样的事情,为什么 msvc 会编译它?
像这样更改代码的最后一部分:
template<typename T, template<typename...> typename Contained, template<typename...> typename Container>
concept is_container_of = is_container_of_if<T, Container, is_a<Contained>::template type>;
它在 clang 和 gcc 中编译。 is_container_of_if
需要一个 模板 作为最后一个模板参数,但您试图传递一个类型。