简体   繁体   English

将表达式折叠为[]运算符的参数

[英]Fold expression as argument of [] operator

I am trying to use a fold expression as the argument of the [] operator. 我试图使用fold表达式作为[]运算符的参数。 Unfortunately, only the first element is correct. 不幸的是,只有第一个元素是正确的。

template <class ...U> T& operator[](U ...indices){
    size_t i=0, k[get_n_dimensions()];
    (... , void(k[i++]=indices));
    // use k[0], k[1], ...
    // only k[0] is correct
}

However, if I use the same syntax for the argument of a function, it works fine. 但是,如果我对函数的参数使用相同的语法,它可以正常工作。

template <class ...U> T get(U ...indices) const {
    size_t i=0, k[get_n_dimensions()];
    (... , void(k[i++]=indices));
    // k[0], k[1], ... filled correctly
}

What is the reason? 是什么原因? What would be the solution? 什么是解决方案?

What is the reason? 是什么原因?

The array subscript operator ( operator[] ) must have exactly one argument. 数组下标运算符( operator[] )必须只有一个参数。 The first snippet you have shown is invalid for any sizeof...(U) != 1 . 您显示的第一个片段对于任何sizeof...(U) != 1都是无效的sizeof...(U) != 1

A function template like get or another operator like operator() do not have a similar limitation. get或其他运算operator()operator()类的函数模板没有类似的限制。


What would be the solution? 什么是解决方案?

Do not use operator[] . 不要使用operator[]

C++ does not have multi-dimensional indexing, and that's "hard-wired" into the syntax - you can't add it by overloading. C ++没有多维索引,并且在语法中“硬连线” - 你不能通过重载来添加它。

The reason that your code compiles at all is that p[a,b] is equivalent to p[(a,b)] - that is, it uses the regular comma operator which will compute a and ignore the result, then produce the value of b as the index into p . 您的代码编译在所有的原因是, p[a,b]等价于p[(a,b)] -即,它使用常规的逗号操作符,其将计算a ,并忽略的结果,则产生值b作为p的索引。

Thus, your template is never used "variadically", but is essentially the same as 因此,您的模板永远不会“变量”使用,但基本上与之相同

template <class U> T& operator[](U indices)

You need to name the function, or overload an operator that can take more than one parameter, such as operator() . 您需要为函数命名,或者重载可以使用多个参数的operator() ,例如operator()

As others have mentioned, the subscripting operator operator[] must accept only a single parameter. 正如其他人所提到的,下标运算符operator[]必须只接受一个参数。

See [over.sub] ( emphasis mine ) 见[over.sub]( 强调我的

operator[] shall be a non-static member function with exactly one parameter . operator[]应该是一个非静态成员函数,只有一个参数 It implements the subscripting syntax 它实现了下标语法

However, that one parameter doesn't have to be an integer. 但是,该参数不必是整数。

You could still do what you want if you are able to move your values into a std::index_sequence like so: 如果您能够将值移动到std::index_sequence您仍然可以执行您想要的操作:

template<size_t... indices>
T& operator[](std::index_sequence<indices...>){
    size_t i=0, k[get_n_dimensions()];
    (... , void(k[i++]=indices));
    // ...
}

You'd call it like so 你这样称呼它

my_instance[std::make_index_sequence<3>{}]; // passes 0, 1, 2 to the function

Live Demo 现场演示

如何使用自定义运算符 &lt; 制作折叠表达式<!--?</div--><div id="text_translate"><p> 我正在尝试根据参数包和折叠表达式打印 function 。 我的实现无法在 clang 10.0 上编译。 这是代码:</p><pre class="lang-cpp prettyprint-override"> #include &lt;iostream&gt; #include &lt;set&gt; std::ostream &amp;operator&lt;&lt;(std::ostream &amp;strm, const std::set&lt;int&gt; &amp;set) { strm &lt;&lt; set.size(); return strm; } template &lt;typename... Args&gt; void Print(std::ostream &amp;strm, Args &amp;&amp;... args) { (strm &lt;&lt;... &lt;&lt; args); } int main(int, char **) { std::set&lt;int&gt; my_set; Print(std::cout, my_set); }</pre><p> 这是编译器 output</p><pre> #1 with x86-64 clang 10.0.0 &lt;source&gt;:10:12: error: call to function 'operator&lt;&lt;' that is neither visible in the template definition nor found by argument-dependent lookup (strm &lt;&lt;... &lt;&lt; args); ^ &lt;source&gt;:15:3: note: in instantiation of function template specialization 'Print&lt;std::set&lt;int, std::less&lt;int&gt;, std::allocator&lt;int&gt; &gt; &amp;&gt;' requested here Print(std::cout, my_set); ^ &lt;source&gt;:4:15: note: 'operator&lt;&lt;' should be declared prior to the call site std::ostream &amp;operator&lt;&lt;(std::ostream &amp;strm, const std::set&lt;int&gt; &amp;set) { ^ 1 error generated. Compiler returned: 1</pre><p> 使用 gcc 10.1 编译相同的代码。 这是编译器错误(或缺少功能),还是我的实现不正确?</p></div> - How do you make a fold expression with custom operator <<?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 &amp;&amp; 运算符连接折叠/可变参数表达式的输出 - Join outputs from fold/variadic expression with && operator 折叠表达式模板参数推断/替换失败 - Fold Expression template argument deduction/substitution failed 在using声明引入的fold表达式中使用运算符是否合法? - Is it legal to use an operator in a fold expression that was introduced by a using declaration? Clang 在折叠表达式中找不到模板二元运算符 - Clang can't find template binary operator in fold expression 使用逗号运算符和可变参数模板参数包折叠表达式 - Fold expression with comma operator and variadic template parameter pack 如何使用自定义运算符 &lt; 制作折叠表达式<!--?</div--><div id="text_translate"><p> 我正在尝试根据参数包和折叠表达式打印 function 。 我的实现无法在 clang 10.0 上编译。 这是代码:</p><pre class="lang-cpp prettyprint-override"> #include &lt;iostream&gt; #include &lt;set&gt; std::ostream &amp;operator&lt;&lt;(std::ostream &amp;strm, const std::set&lt;int&gt; &amp;set) { strm &lt;&lt; set.size(); return strm; } template &lt;typename... Args&gt; void Print(std::ostream &amp;strm, Args &amp;&amp;... args) { (strm &lt;&lt;... &lt;&lt; args); } int main(int, char **) { std::set&lt;int&gt; my_set; Print(std::cout, my_set); }</pre><p> 这是编译器 output</p><pre> #1 with x86-64 clang 10.0.0 &lt;source&gt;:10:12: error: call to function 'operator&lt;&lt;' that is neither visible in the template definition nor found by argument-dependent lookup (strm &lt;&lt;... &lt;&lt; args); ^ &lt;source&gt;:15:3: note: in instantiation of function template specialization 'Print&lt;std::set&lt;int, std::less&lt;int&gt;, std::allocator&lt;int&gt; &gt; &amp;&gt;' requested here Print(std::cout, my_set); ^ &lt;source&gt;:4:15: note: 'operator&lt;&lt;' should be declared prior to the call site std::ostream &amp;operator&lt;&lt;(std::ostream &amp;strm, const std::set&lt;int&gt; &amp;set) { ^ 1 error generated. Compiler returned: 1</pre><p> 使用 gcc 10.1 编译相同的代码。 这是编译器错误(或缺少功能),还是我的实现不正确?</p></div> - How do you make a fold expression with custom operator <<? 用 || 折叠函数操作员 - Fold over a function with || operator 用运算符折叠表达式 &gt;&gt; - Fold expressions with operator >> 折叠表达 - fold expression in assignment 枚举折叠表达式 - Enumerating over a fold expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM