简体   繁体   English

模板特化中的多个 void_t 调用

[英]Multiple void_t calls in template specializations

The first thing I need help with is to resolve the ambiguity below.我需要帮助的第一件事是解决下面的歧义。 But once the ambiguity is gone, I still need to know if there is a more concise and elegant way to implement the 8 specializations.但是一旦歧义消失,我仍然需要知道是否有更简洁优雅的方式来实现 8 个专业化。

#include <iostream>

template <typename>
using void_t = void;

template <typename T, typename U, typename = void, typename = void, typename = void>
struct Foo {
    static void call() {std::cout << "Case 1\n";}
};

template <typename T, typename U>
struct Foo<T, U,
        void_t<decltype(std::declval<T>().foo(int()))>,
        void_t<decltype(std::declval<U>().bar(bool(), char()))>,
        void_t<decltype(execute(std::declval<const T&>(), std::declval<const U&>()))>> {
    static void call() {std::cout << "Case 2\n";}
};

template <typename T, typename U>
struct Foo<T, U,
        void_t<decltype(std::declval<T>().foo(int()))>,
        void, void> {
    static void call() {std::cout << "Case 3\n";}
};
// etc... for the remaining 5 specializations.

struct Thing {
    void foo(int) {}
};

struct Uber {
    int bar(bool, char) {return 2;}
};

void execute (const Thing&, const Uber&) {}

int main() {
    Foo<Thing, int>::call();  // Case 3
//  Foo<Thing, Uber>::call();  // Ambiguous.  Want this to be "Case 2" instead of "Case 3".
}

So first, I need to know why Foo<Thing, Uber>::call();所以首先,我需要知道为什么Foo<Thing, Uber>::call(); is ambiguous.是模棱两可的。 All 3 void_t's are fulfilled, so isn't Case 2 more specialized than Case 3?所有 3 个 void_t 都得到满足,所以案例 2 不是比案例 3 更专业吗? Also, I intend to have 5 more specializations for the 2x2x2 possibilities of the 3 void_t's being fulfilled or not fulfilled.另外,我打算针对 3 个 void_t 实现或未实现的 2x2x2 可能性再增加 5 个专业化。 What is the most elegant way to handle 2^n such specializations if there were n void_t's being used?如果使用了 n 个 void_t,那么处理 2^n 个这样的专业化的最优雅的方法是什么?

As an analogy, for the case of handling 3 std::enable_if_t calls like打个比方,对于处理 3 个std::enable_if_t调用的情况,如

#include <iostream>
#include <type_traits>

template <bool B> using bool_constant = std::integral_constant<bool, B>;

template <std::size_t N>
struct is_even : bool_constant<N % 2 == 0> {};

template <std::size_t N, std::size_t A>
struct add_to_odd : bool_constant<(N + A) % 2 == 1> {};

template <std::size_t N, std::size_t A, std::size_t B>
struct is_one_of_these : bool_constant<N == A || N == B> {};

template <std::size_t N, std::size_t A, std::size_t B, typename = void, typename = void, typename = void>
struct Foo {
    static void call() {std::cout << "Case 1\n";}
};

template <std::size_t N, std::size_t A, std::size_t B>
struct Foo<N,A,B,
        std::enable_if_t<is_even<N>::value>, 
        std::enable_if_t<!add_to_odd<N,A>::value>, 
        std::enable_if_t<!is_one_of_these<N,A,B>::value>> {
    static void call() {std::cout << "Case 2\n";}
};

// etc... for the other combinations of the 3 enable_if conditions being true/false.

int main() {
    Foo<1,2,3>::call();
    Foo<8,2,3>::call();
}

I figured out我想通了

#include <iostream>
#include <type_traits>

template <bool B> using bool_constant = std::integral_constant<bool, B>;

template <std::size_t N>
struct is_even : bool_constant<N % 2 == 0> {};

template <std::size_t N, std::size_t A>
struct add_to_odd : bool_constant<(N + A) % 2 == 1> {};

template <std::size_t N, std::size_t A, std::size_t B>
struct is_one_of_these : bool_constant<N == A || N == B> {};

template <std::size_t N, std::size_t A, std::size_t B, bool, bool, bool>
struct FooHelper {
    static void call() {std::cout << "Case 1\n";}
};

template <std::size_t N, std::size_t A, std::size_t B>
struct FooHelper<N, A, B, true, false, false> {
    static void call() {std::cout << "Case 2\n";}
};

template <std::size_t N, std::size_t A, std::size_t B>
struct FooHelper<N, A, B, true, true, false> {
    static void call() {std::cout << "Case 3\n";}
};

template <std::size_t N, std::size_t A, std::size_t B>
struct FooHelper<N, A, B, true, true, true> {
    static void call() {std::cout << "Case 4\n";}
};

template <std::size_t N, std::size_t A, std::size_t B>
struct FooHelper<N, A, B, false, true, true> {
    static void call() {std::cout << "Case 5\n";}
};

template <std::size_t N, std::size_t A, std::size_t B>
struct FooHelper<N, A, B, false, false, true> {
    static void call() {std::cout << "Case 6\n";}
};

template <std::size_t N, std::size_t A, std::size_t B>
struct FooHelper<N, A, B, true, false, true> {
    static void call() {std::cout << "Case 7\n";}
};

template <std::size_t N, std::size_t A, std::size_t B>
struct FooHelper<N, A, B, false, true, false> {
    static void call() {std::cout << "Case 8\n";}
};

template <std::size_t N, std::size_t A, std::size_t B>
struct Foo : FooHelper<N, A, B, is_even<N>::value, add_to_odd<N,A>::value, is_one_of_these<N,A,B>::value> {};

int main() {
    Foo<1,2,3>::call();
    Foo<8,2,3>::call();
    // etc...
}

to make the 8 template specializations more concise, maintainable, and easier to read.使 8 个模板专业化更简洁、可维护且更易于阅读。 But the analogy for n void_t's is not so obvious to me (well, the above ambiguity doesn't really help either).但是 n void_t's 的类比对我来说并不是那么明显(好吧,上述歧义也没有真正帮助)。

In your case, both case #2 and case #3 are equally specialized, you are just writing it differently void_t<decltype(std::declval<U>().bar(bool(), char()))> is void if decltype does not fail.在您的情况下,案例 #2 和案例 #3 同样专业,您只是写得不同void_t<decltype(std::declval<U>().bar(bool(), char()))> is void如果 decltype 没有失败。

You could add additional flag to specify, which specialization you want to use.您可以添加额外的标志来指定要使用的专业化。 Alternativelly, you can resolve ambiguities with conversion sequence ranking.或者,您可以通过转换序列排名解决歧义。

#include <iostream>

template <typename>
using void_t = void;

template <int P>
struct Rank : Rank<P - 1> {};

template <>
struct Rank<0> : std::integral_constant<int, 0> {};

// Helper functions
template <typename T>
static auto has_foo_impl(int)
    -> decltype(std::declval<T>().foo(int()), std::true_type());
template <typename T>
static auto has_foo_impl(long) -> std::false_type;

template <typename T>
constexpr bool has_foo() {
  return std::is_same<decltype(has_foo_impl<T>(0)), std::true_type>::value;
};

template <typename T>
static auto has_bar_impl(int)
    -> decltype(std::declval<T>().bar(bool(), char()), std::true_type());
template <typename T>
static auto has_bar_impl(long) -> std::false_type;

template <typename T>
constexpr bool has_bar() {
  return std::is_same<decltype(has_bar_impl<T>(0)), std::true_type>::value;
};

template <typename T, typename U>
static auto has_execute_impl(int)
    -> decltype(execute(std::declval<T&>(), std::declval<U&>()),
                std::true_type());
template <typename T, typename U>
static auto has_execute_impl(long) -> std::false_type;

template <typename T, typename U>
constexpr bool has_execute() {
  return std::is_same<decltype(has_execute_impl<T, U>(0)),
                      std::true_type>::value;
};

// Call overloads
template <typename T, typename U>
static void call_impl(Rank<0>) {
  std::cout << "Case 1\n";
}

template <typename T, typename U>
static auto call_impl(Rank<5>)
    -> std::enable_if_t<has_foo<T>() && has_bar<U>() && has_execute<T, U>()> {
  std::cout << "Case 2\n";
}

template <typename T, typename U>
static auto call_impl(Rank<4>) -> std::enable_if_t<has_foo<T>()> {
  std::cout << "Case 3\n";
}

template <typename T, typename U>
struct Foo {
  static void call() { call_impl<T, U>(Rank<10>()); }
};

struct Thing {
  void foo(int) {}
};

struct Uber {
  int bar(bool, char) { return 2; }
};

void execute(const Thing&, const Uber&) {}

int main() {
  Foo<Thing, int>::call();   // Case 3
  Foo<Thing, Uber>::call();  // Ambiguous.  Want this to be "Case 2" instead of
                             // "Case 3".
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM