本文介绍了将枚举值与 SFINAE 结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经熟悉 SFINAE 以及如何使用它根据传递的类型启用特定模板(通过使用 std::enable_if).但是,我最近开始从事一个项目,我想在其中执行以下操作:在使用 SFINAE 时根据提供的枚举 VALUE 创建一个类专业化.现在,我知道考虑到我之前已经这样做过(就像这样),可以根据枚举值进行专业化:

I am already familiar with SFINAE and how it can be used to enable specific templates based on the passed type (by using std::enable_if). However I have recently started working on a project where I'd like to do the following: create a class specialization based on the provided enum VALUE while using SFINAE. Now, I know that it's possible to make specializations based on an enum value considering I've done this before (like so):

enum Specifier
{
   One,
   Two,
   Three
}

template <Specifier>
class Foo
{
public:
   void Bar();
}

template<>
void Foo<Specifier::One>::Bar()
{
}

但是现在我想使用 SFINAE 对多个枚举值使用 Bar() 的特定专业化.像这样:

However now I'd like to use SFINAE to use a specific specialization of Bar() for multiple enum values. Something like this:

template <Specifier Type>
class Foo
{
public:
   template <typename std::enable_if<Type == Specifier::Two || Type == Specifier::One, void>::type>
   void Bar();

   template <typename std::enable_if<Type == Specifier::Three, void>::type>
   void Bar();
}

知道这是否可行,如果可行,我将如何进行?

Any idea if this is possible, and if so, how would I go about doing this?

推荐答案

C++17: constexpr if

从 C++17 开始,您可以使用单个成员函数重载(而不是通过 SFINAE 存在或不存在的多个重载),其主体在以下情况下利用 constexpr:

C++17: constexpr if

From C++17 and onwards, you could use a single member function overload (instead of several overloads present or not present via SFINAE) whose body leverages constexpr if:

#include <iostream>

enum class Specifier { One, Two, Three };

template <Specifier S> class Foo {
public:
  static constexpr int bar() {
    if constexpr ((S == Specifier::One) || (S == Specifier::Two)) {
      return 12;
    } else if constexpr (S == Specifier::Three) {
      return 3;
    }
  }
};

int main() {
  std::cout << Foo<Specifier::One>::bar() << "\n" // 12
            << Foo<Specifier::Two>::bar() << "\n" // 12
            << Foo<Specifier::Three>::bar();      // 3
}


C++11:SFINAE 和 std::enable_if(_t) (C++14)

您同样可以使用 SFINAE,但要求您的非模板成员函数需要成为具有虚拟模板参数的成员函数模板,因为 SFINAE 需要应用于每个中的依赖名称函数声明,并且类模板(类型或非类型)参数自然不是非模板成员函数声明中的依赖名称:


C++11: SFINAE and std::enable_if(_t) (C++14)

You can likewise use SFINAE with the requirement that your non-template member functions need to be made member function templates with a dummy template parameter, as SFINAE needs to be applied to a dependent name in each function declaration, and a class template (type or non-type) parameter is naturally not a dependent name in the declaration of a non-template member function:

template <Specifier S> class Foo {
public:
  template <Specifier S_ = S,
            std::enable_if_t<(S_ == Specifier::One) || (S_ == Specifier::Two)>
                * = nullptr>
  static constexpr int bar() {
    return 12;
  }

  template <Specifier S_ = S,
            std::enable_if_t<(S_ == Specifier::Three)> * = nullptr>
  static constexpr int bar() {
    return 3;
  }
};

请注意,上面的示例使用了 C++14 中引入的帮助程序别名模板 std::enable_if_t.如果您使用的是 C++11,则需要使用 typename std::enable_if<..>::type 代替.

Note that the example above uses the helper alias template std::enable_if_t that was introduced in C++14. If you are using C++11, you will need to use typename std::enable_if<..>::type instead.

此外请注意,由于我们必须模板化成员函数,滥用用户可以选择覆盖(虚拟)非类型模板参数的默认模板参数S_:

Moreover note that as we have to templetize the member functions an abusive user could choose to override the default template argument for the (dummy) non-type template parameter S_:

Foo<Specifier::One>::bar<Specifier::Three>();  // 3

所以我们可能想为每个重载的 std::enable_if_t 谓词添加一个额外的 AND 条件,即 (S_ == S) &&(...谓词如上).正如我们将在接下来的部分中看到的,这在 C++20 中不再是一个问题,因为我们可以避免将非模板成员函数变成模板,仅用于应用 SFINAE.

So we may want to add an additional AND condition to the std::enable_if_t predicate for each overload, namely (S_ == S) && (... predicate as above). As we shall see in the section that follows, this is no longer an issue in C++20, as we can avoid making non-template member functions into templates solely for applying SFINAE.

替代使用专业化而不是重载

正如我在以下答案中针对此问题的后续问题所示,您也可以申请特化的模板参数列表中的 SFINAE(到部分特化的类模板):

As I've also shown in the following answer to a follow up question to this question, you may also apply SFINAE in the template argument list (to the class template being partially specialized) of a specialization:

template <Specifier, typename = void> struct Foo {
  static constexpr int bar() { return 1; }  // default
};

template <Specifier S>
struct Foo<S,
           std::enable_if_t<(S == Specifier::One) || (S == Specifier::Two)>> {
  static constexpr int bar() { return 12; }
};


C++20:类模板的非模板成员函数可以使用requires-clause:s

从 C++20 开始,您可以使用尾随的 requires-clause 重载和约束类模板的非模板成员函数,并为每个重载提供互斥约束:


C++20: non-template member functions of class templates may use requires-clause:s

As of C++20, you can overload and constrain a non-template member function of a class template using a trailing requires-clause with mutual exclusive constraints for each overloaded:

template <Specifier S> class Foo {
public:
  static constexpr int bar() requires((S == Specifier::One) ||
                                      (S == Specifier::Two)) {
    return 12;
  }

  static constexpr int bar() requires(S == Specifier::Three) { return 3; }
};

这篇关于将枚举值与 SFINAE 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 01:26