有没有办法从"std::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;
}

但是编译器似乎不明白我正在尝试调用operator""_lit({10, 20, 30, 40});有什么方法可以解决它吗?


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

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

主要思想是简化这一点:

if ((val_1 == value) && (val_2 == value) && (val_3 == value)) { /* ... */ }

进入一些沿着线:

if (std::initializer_list<T>{val_1, val_2, val_3} == value)

ofc 我提供了一个额外的运算符:

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;
}

一切都会很好,但我不喜欢在大括号前输入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)

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)>;

测试代码:

if (any_of{1,2,3} == 2) {
std::cout << "2 is theren";
}
if (! (any_of{1,2,3} == 7) ){
std::cout << "7 is not theren";
}
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";
}

活生生的例子。

c++17 编译器中的输出:

2 is there
7 is not there
overlap!
no overlap!

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

十字型双any_of,如:

any_of{1,2,3} == any_of{3.14, 5.7, 1.0}

将无法编译,因为两者==any_of工作。

不能为std::initializer_list创建用户定义的文本。 幸运的是,C++17提供了一个非常酷的新工具,可以帮助我们。 类模板参数推导允许我们只使用类模板的名称,编译器将找出模板参数需要是什么,这样我们就不必指定它们。 这意味着你可以利用std::array,你的代码将成为

template<typename T, std::size_t N>
bool operator==(std::array<T, N> const & list, T const & ref)
{
for(auto const& e : list)
if (e != ref)
return false;
return true;
}
int main()
{
using std::array;
if (array{4,4,4,4,4} == 4)
std::cout << "all 4";
}

除了 using 语句之外,它只是_litarray之间的一个额外字符

来自评论:

@NathanOliver 我正在尝试"扩展"当前的C++语法(这很有趣) 小项目...主要思想是简化这一点:如果(val_1== value) && (val_2 == value) && (val_3 == value)) 变成这样:如果 (std::initializer_list{val_1, val_2, val_3} == 值)(带有 重载运算符:bool 运算符==(std::initializer_list const & list, T const & ref))...我想省略我需要的部分 键入 std::initializer_list 我发现我可以更改它 变成自定义文字

所以看起来你需要这样的东西:

template<typename T>
bool allEqualTo(T a, T b)
{
return a == b;
}
template<typename T, typename... TArgs>
bool allEqualTo(T a, T b, TArgs... args)
{
return allEqualTo(a, b) && allEqualTo(a, args...);
}
if (allEqualTo(value, val_1, val_2, val_3)) {
....
}

如果使用范围库,则只需使用all_of

// Using Range-v3: https://ericniebler.github.io/range-v3/index.html
if (ranges::v3::all_of({val_1, val_2, val_3},
[value](auto const& other) { return value == other; })) {
// ...
}

您可以使用帮助程序进一步简化它:

// Note: Prior to C++17, this could lead to ODR violations.
// After C++17, this will be an inline variable, thus this is fine.
// If using in C++14 or before, write std::equal_to<>{} instead of std::equal_to{}.
constexpr auto equal_to = boost::hof::partial(std::equal_to{});
// ...
if (ranges::v3::all_of({val1, val_2, val_3}, equal_to(value))) {
// ...
}

演示

最新更新