确定编译时参数的唯一性

时间:2018-07-14 19:02:17

标签: c++ templates c++14 variadic-templates template-meta-programming

问题

我有以下类似容器的数组:

template <
    class _Tp,
    size_t _Size
> class Alphabet {
public:
    template <class... _Ts>
    constexpr
    Alphabet(_Ts&& ... ts) 
        : m_data(std::forward<_Ts>(ts)...})
    {}
private:
    _Tp m_data[_Size ? _Size : 1];
}

我这样使用:

constexpr Alphabet<char, 3> abc{'a', 'b', 'c'}; // ok, elements are distinct

但是,我希望能够在编译时告诉 元素是否唯一。因此,在构造函数的主体中,我只需添加:

Alphabet(_Ts&& ... ts) 
    : m_data{std::forward<_Ts>(ts)...}
{
    static_assert(is_unique(ts...), "Elements must be distinct!")
}

所以现在我写:

constexpr Alphabet<char, 3> abc{'a', 'b', 'b'}; // error! elements are not distinct

我尝试过的事情

为此,我编写了以下代码,这些代码在运行时有效:

template <class _Tp>
constexpr
bool is_one_of(_Tp)
{
    return false;
}

template <class _Tp, class... _Ts>
constexpr
bool is_one_of(_Tp t, _Tp f, _Ts... ts)
{
    return (t == f) || is_one_of(f, ts...);
}

template <class _Tp>
constexpr
bool is_unique(_Tp)
{
    return true;
}

template <class _Tp, class... _Ts>
constexpr
bool is_unique(_Tp t, _Ts... ts)
{   
    return is_unique(ts...) && !is_one_of(t, ts...);
}

不幸的是,在尝试编译时,我很快会遇到以下错误:

error: non-constant condition for static assertion
     static_assert(detail::is_unique(ts...),
                                     ^
error: 'ts#0' is not a constant expression

我正在使用C ++ 14。任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

如果要检查ts...值的编译时间(static_assert()),则必须将其作为曾经编译时已知的值进行传递。

您无法检查constexpr构造函数的编译时参数,因为该构造函数还可以与运行时值一起使用。

我的建议是将ts...值作为模板参数传递,因此它们曾经是已知的编译时间,因此您可以在static_assert()测试中对其进行检查。

例如:如果Tp是整数类型,则可以将它们作为std::integer_sequence的参数传递

  template <Tp ... Ts>
  constexpr Alphabet (std::integer_sequence<Tp, Ts...> const &)
     : m_data{Ts...}
   { static_assert(    sizeof...(Ts) <= Size
                    && are_unique<Ts...>(), "!" ); }

关于are_unique(),我提出了一个递归版本

  // ground case
  template <typename = void>
  static constexpr bool are_unique ()
   { return true; }

  // recursive case
  template <Tp T0, Tp ... Ts>
  static constexpr bool are_unique ()
   { return is_unique<T0, Ts...>() && are_unique<Ts...>(); }

关于is_unique(),您可以使用unused数组初始化的技巧

  template <Tp T0, Tp ... Ts>
  static constexpr bool is_unique ()
   {
     using unused = bool[];

     bool ret = true;

     (void)unused{ false, ret &= T0 != Ts... };

     return ret;
   }

如果您不希望每次都编写std::integer_sequence部分,则只能在make_Alphabet()模板函数中一次编写一次

template <typename Tp, Tp ... Ts>
constexpr Alphabet<Tp, sizeof...(Ts)> make_Alphabet ()
 { return { std::integer_sequence<Tp, Ts...>{} }; }

并按以下方式使用

constexpr auto abcd{ make_Alphabet<char, 'a', 'b', 'c', 'd'>() };

以下是完整的C ++ 14示例

#include <utility>
#include <iostream>

template <typename Tp, std::size_t Size>
class Alphabet
 {
   private:
      Tp m_data[Size ? Size : 1U];

      template <Tp T0, Tp ... Ts>
      static constexpr bool is_unique ()
       {
         using unused = bool[];

         bool ret = true;

         (void)unused{ false, ret &= T0 != Ts... };

         return ret;
       }

      // ground case
      template <typename = void>
      static constexpr bool are_unique ()
       { return true; }

      // recursive case
      template <Tp T0, Tp ... Ts>
      static constexpr bool are_unique ()
       { return is_unique<T0, Ts...>() && are_unique<Ts...>(); }

   public:
      template <Tp ... Ts>
      constexpr Alphabet (std::integer_sequence<Tp, Ts...> const &)
         : m_data{Ts...}
       { static_assert(    sizeof...(Ts) <= Size
                        && are_unique<Ts...>(), "!" ); }
 };

template <typename Tp, Tp ... Ts>
constexpr Alphabet<Tp, sizeof...(Ts)> make_Alphabet ()
 { return { std::integer_sequence<Tp, Ts...>{} }; }

int main()
 {
   // compilation error (static_assert failed "!")
   //constexpr Alphabet<char, 3>
   //   aba{std::integer_sequence<char, 'a', 'b', 'a'>{}};

   // compile
   constexpr Alphabet<char, 3>
      abc{std::integer_sequence<char, 'a', 'b', 'c'>{}};

   // compilation error (static_assert failed "!")
   //constexpr auto abca{ make_Alphabet<char, 'a', 'b', 'c', 'a'>() };

   // compile
   constexpr auto abcd{ make_Alphabet<char, 'a', 'b', 'c', 'd'>() };
 }
相关问题