有没有办法从`std :: initializer_list`创建用户定义的文字?

Pat*_*ykB 5 c++ literals initializer-list

就像在主题中一样:有没有办法从中创建用户定义的文字std::initializer_list

我想做那样的事情:

template <typename T> inline
std::initializer_list<T> const & operator "" _lit(std::initializer_list<T> const & list)
{
    return std::move(list); // I am not sure, but this line might cause undefined behavior... well I'll think about it latter...
}

int main()
{
    { 10, 20, 30, 40 }_lit // Error: identifier '_lit' is undefined;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但似乎编译器不明白我试图打电话operator""_lit({10, 20, 30, 40});有没有办法解决它?


编辑:
对不起,原来这只是XY问题的另一个例子......
让我详细说明

我试图"扩展"当前的C++语法(这是一个有趣的小项目...)

主要想法是简化这个:

if ((val_1 == value) && (val_2 == value) && (val_3 == value)) { /* ... */ }
Run Code Online (Sandbox Code Playgroud)

进入某些方面:

if (std::initializer_list<T>{val_1, val_2, val_3} == value)
Run Code Online (Sandbox Code Playgroud)

我正在提供一个额外的运营商:

template <typename T> inline
bool operator==(std::initializer_list<T> const & list, T const & ref)
{
    for (auto const & element : list)
    {
        if (element == ref) { /* Do nothing. */ }
        else
        {
            return false;
        }
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

一切都会很好很好,但我不喜欢在std::initializer_list<T>大括号前键入...否则,编译器会选择默认版本,operator==()我会得到一个编译错误...

字面来这里作为一个想法改变if (std::initializer_list<T>{val_1, val_2, val_3} == value)if ({val_1, val_2, val_3}_lit == value)

Yak*_*ont 1

template<class T, std::size_t N>
struct any_of:std::array<T, N> {
  #define MAKE_OPERATOR( OPERATOR ) \
    template<class U, \
      std::enable_if_t< std::is_same<void, std::void_t< \
        decltype( std::declval<T const&>() == std::declval<U const&>() ) \
      >>{}, bool> =true \
    > \
    friend bool operator OPERATOR ( any_of const& lhs, U const& rhs) { \
      return std::any_of( \
        lhs.begin(), lhs.end(), \
        [&](auto&& lhs){ return lhs OPERATOR rhs; } \
      ); \
    } \
    template<class U, \
      std::enable_if_t< std::is_same<void, std::void_t< \
        decltype( std::declval<U const&>() == std::declval<T const&>() ) \
      >>{} && !std::is_same< U, any_of >{} , bool> =true \
    > \
    friend bool operator OPERATOR ( U const& lhs, any_of const& rhs) { \
      return std::any_of( \
        rhs.begin(), rhs.end(), \
        [&](auto&& rhs){ return lhs OPERATOR rhs; } \
      ); \
    }
  MAKE_OPERATOR(==)
  MAKE_OPERATOR(!=)
  MAKE_OPERATOR(<)
  MAKE_OPERATOR(<=)
  MAKE_OPERATOR(>=)
  MAKE_OPERATOR(>)
  #undef MAKE_OPERATOR
  explicit any_of( std::array<T, N> arr):std::array<T, N>(std::move(arr)) {}
  template<class...Ts>
  explicit any_of( T t, Ts... ts ):std::array<T, N>{ std::move(t), std::move(ts)... } {}
  any_of( any_of const& )=delete;
  any_of& operator=( any_of const& )=delete;
  any_of()=delete;
};
template<class T, std::size_t N>
any_of(T(&)[N]) -> any_of<T,N>;
template<class T, class...Ts>
any_of(T, Ts...) -> any_of<T, 1+sizeof...(Ts)>;
Run Code Online (Sandbox Code Playgroud)

测试代码:

if (any_of{1,2,3} == 2) {
    std::cout << "2 is there\n";
}
if (! (any_of{1,2,3} == 7) ){
    std::cout << "7 is not there\n";
}

if (any_of{1,2,3} == any_of{5,6,1}) {
    std::cout << "overlap!\n";
}
if (!(any_of{1,2,3} == any_of{5,6,7})) {
    std::cout << "no overlap!\n";
}
Run Code Online (Sandbox Code Playgroud)

活生生的例子

编译器的输出:

2 is there
7 is not there
overlap!
no overlap!
Run Code Online (Sandbox Code Playgroud)

各种比较运算符都受支持。

交叉类型 double any_of,如:

any_of{1,2,3} == any_of{3.14, 5.7, 1.0}
Run Code Online (Sandbox Code Playgroud)

将无法编译,因为两者都==有效any_of