用户定义文字参数中的初始化器列表

时间:2021-02-27 22:45:04

I don't know if it's possible but I want to do stuff like

我不知道这是否可能,但我想做类似的事情

int someval = 1;
if({1,2,3,4}_v.contains(someval ))

but when I try to define literal as:

但是当我试图把文字定义为:

std::vector<int> operator"" _v ( std::initializer_list<int> t )
{
    return std::vector<int> (t);
}

to accept initializer list of ints I get

要接受我得到的ints的初始化器列表

 error: 'std::vector<int> operator"" _v(std::initializer_list<int> t)' has invalid argument list

Is there a way to do this? What I really want is to finally be rid of stuff like

有办法吗?我真正想要的是最终摆脱像这样的东西

if(value == 1 || value ==2 || value == 3 ...

Having to write stuff like this is really annoying, because you'd expect syntax to be

必须写这样的东西真的很烦人,因为你会认为语法是

if value in (value1, value2 ...) 

or something similar.

或类似的东西。

3 个解决方案

#1


6  

you'd expect syntax to be

语法应该是

if value in (value1, value2 ...) 

or something similar.

或类似的东西。

If you're willing to add one extra character, try this syntax:

如果你想增加一个额外的字符,试试这个语法:

#include <algorithm>
#include <iostream>
#include <array>

template <typename T0, typename T1, std::size_t N>
bool operator *(const T0& lhs, const std::array<T1, N>& rhs) {
  return std::find(begin(rhs), end(rhs), lhs) != end(rhs);
}

template<class T0, class...T> std::array<T0, 1+sizeof...(T)> in(T0 arg0, T...args) {
  return {{arg0, args...}};
}

int main () {
  if( 2 *in(1,2,3) ) { std::cout << "Hello\n"; }
  if( 4 *in(5,6,7,8) ) { std::cout << "Goodbye\n"; }
}

#2


7  

How about this:

这个怎么样:

#include <initializer_list>

template <typename T>
bool contains(std::initializer_list<T> const & il, T const & x)
{
    for (auto const & z : il) { if (z == x) return true; }
    return false;
}

Usage:

用法:

bool b = contains({1, 2, 3}, 5);  // false

#3


4  

§13.5.8/3 says:

§13.5.8/3说:

The declaration of a literal operator shall have a parameter-declaration-clause equivalent to one of the following:

文字操作符的声明应具有与下列内容之一等价的参数声明条款:

const char*
unsigned long long int
long double
char
wchar_t
char16_t
char32_t
const char*, std::size_t
const wchar_t*, std::size_t
const char16_t*, std::size_t
const char32_t*, std::size_t

So it looks like you can't have a parameter of initializer_list type.

看起来你不能有initializer_list类型的参数。

I can only think of the obvious as an alternative; if you don't mind typing a little more you can do something like

我只能把显而易见的作为另一种选择;如果你不介意多打一点,你可以做一些类似的事情

std::vector<int> v(std::initializer_list<int> l) {
    return { l };
}

int someval = 1;
if(v({1,2,3,4}).contains(someval))

Alternatively you could get wacky and write an operator overload for initializer_list (haven't tested though):

或者,您也可以为initizerali_list编写一个操作符重载(虽然还没有经过测试):

bool operator<=(std::intializer_list<int> l, int value) {
    return std::find(std::begin(l), std::end(l), value) != std::end(l);
}

And

if ({1, 2, 3, 4} <= 3)

should work...

应该工作……

Actually nevermind, it doesn't. You'll have to go with a normal function.

其实无所谓,它不是。你必须用一个普通的函数。

#1


6  

you'd expect syntax to be

语法应该是

if value in (value1, value2 ...) 

or something similar.

或类似的东西。

If you're willing to add one extra character, try this syntax:

如果你想增加一个额外的字符,试试这个语法:

#include <algorithm>
#include <iostream>
#include <array>

template <typename T0, typename T1, std::size_t N>
bool operator *(const T0& lhs, const std::array<T1, N>& rhs) {
  return std::find(begin(rhs), end(rhs), lhs) != end(rhs);
}

template<class T0, class...T> std::array<T0, 1+sizeof...(T)> in(T0 arg0, T...args) {
  return {{arg0, args...}};
}

int main () {
  if( 2 *in(1,2,3) ) { std::cout << "Hello\n"; }
  if( 4 *in(5,6,7,8) ) { std::cout << "Goodbye\n"; }
}

#2


7  

How about this:

这个怎么样:

#include <initializer_list>

template <typename T>
bool contains(std::initializer_list<T> const & il, T const & x)
{
    for (auto const & z : il) { if (z == x) return true; }
    return false;
}

Usage:

用法:

bool b = contains({1, 2, 3}, 5);  // false

#3


4  

§13.5.8/3 says:

§13.5.8/3说:

The declaration of a literal operator shall have a parameter-declaration-clause equivalent to one of the following:

文字操作符的声明应具有与下列内容之一等价的参数声明条款:

const char*
unsigned long long int
long double
char
wchar_t
char16_t
char32_t
const char*, std::size_t
const wchar_t*, std::size_t
const char16_t*, std::size_t
const char32_t*, std::size_t

So it looks like you can't have a parameter of initializer_list type.

看起来你不能有initializer_list类型的参数。

I can only think of the obvious as an alternative; if you don't mind typing a little more you can do something like

我只能把显而易见的作为另一种选择;如果你不介意多打一点,你可以做一些类似的事情

std::vector<int> v(std::initializer_list<int> l) {
    return { l };
}

int someval = 1;
if(v({1,2,3,4}).contains(someval))

Alternatively you could get wacky and write an operator overload for initializer_list (haven't tested though):

或者,您也可以为initizerali_list编写一个操作符重载(虽然还没有经过测试):

bool operator<=(std::intializer_list<int> l, int value) {
    return std::find(std::begin(l), std::end(l), value) != std::end(l);
}

And

if ({1, 2, 3, 4} <= 3)

should work...

应该工作……

Actually nevermind, it doesn't. You'll have to go with a normal function.

其实无所谓,它不是。你必须用一个普通的函数。