<small id='D2Wyz'></small><noframes id='D2Wyz'>

  1. <tfoot id='D2Wyz'></tfoot>

      • <bdo id='D2Wyz'></bdo><ul id='D2Wyz'></ul>

    1. <i id='D2Wyz'><tr id='D2Wyz'><dt id='D2Wyz'><q id='D2Wyz'><span id='D2Wyz'><b id='D2Wyz'><form id='D2Wyz'><ins id='D2Wyz'></ins><ul id='D2Wyz'></ul><sub id='D2Wyz'></sub></form><legend id='D2Wyz'></legend><bdo id='D2Wyz'><pre id='D2Wyz'><center id='D2Wyz'></center></pre></bdo></b><th id='D2Wyz'></th></span></q></dt></tr></i><div id='D2Wyz'><tfoot id='D2Wyz'></tfoot><dl id='D2Wyz'><fieldset id='D2Wyz'></fieldset></dl></div>

    2. <legend id='D2Wyz'><style id='D2Wyz'><dir id='D2Wyz'><q id='D2Wyz'></q></dir></style></legend>

      使用带有 STL 算法的本地类

      Using local classes with STL algorithms(使用带有 STL 算法的本地类)

        <small id='2OXqI'></small><noframes id='2OXqI'>

          <tbody id='2OXqI'></tbody>
          <legend id='2OXqI'><style id='2OXqI'><dir id='2OXqI'><q id='2OXqI'></q></dir></style></legend>
          <tfoot id='2OXqI'></tfoot>
            <bdo id='2OXqI'></bdo><ul id='2OXqI'></ul>

                <i id='2OXqI'><tr id='2OXqI'><dt id='2OXqI'><q id='2OXqI'><span id='2OXqI'><b id='2OXqI'><form id='2OXqI'><ins id='2OXqI'></ins><ul id='2OXqI'></ul><sub id='2OXqI'></sub></form><legend id='2OXqI'></legend><bdo id='2OXqI'><pre id='2OXqI'><center id='2OXqI'></center></pre></bdo></b><th id='2OXqI'></th></span></q></dt></tr></i><div id='2OXqI'><tfoot id='2OXqI'></tfoot><dl id='2OXqI'><fieldset id='2OXqI'></fieldset></dl></div>

              1. 本文介绍了使用带有 STL 算法的本地类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我一直想知道为什么不能使用本地定义的类作为 STL 算法的谓词.

                I have always wondered why you cannot use locally defined classes as predicates to STL algorithms.

                在问题中:接近STL算法,lambda,本地类和其他方法,BubbaT 提到由于 C++ 标准禁止将本地类型用作参数"

                In the question: Approaching STL algorithms, lambda, local classes and other approaches, BubbaT mentions says that 'Since the C++ standard forbids local types to be used as arguments'

                示例代码:

                int main() {
                   int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                   std::vector<int> v( array, array+10 );
                
                   struct even : public std::unary_function<int,bool>
                   {
                      bool operator()( int x ) { return !( x % 2 ); }
                   };
                   std::remove_if( v.begin(), v.end(), even() ); // error
                }
                

                有谁知道标准中的限制在哪里?禁止本地类型的理由是什么?

                Does anyone know where in the standard is the restriction? What is the rationale for disallowing local types?

                编辑:从 C++11 开始,使用本地类型作为模板参数是合法的.

                EDIT: Since C++11, it is legal to use a local type as a template argument.

                推荐答案

                C++98/03 标准明确禁止这样做.

                It's explicitly forbidden by the C++98/03 standard.

                C++11 移除了这个限制.

                C++11 remove that restriction.

                为了更完整:

                对类型的限制用作模板参数列出在 C++03 的第 14.3.1 条(和C++98) 标准:

                The restrictions on types that are used as template parameters are listed in article 14.3.1 of the C++03 (and C++98) standard:

                一个本地类型,一个没有链接的类型,未命名类型或复合类型从任何这些类型不得用作模板参数模板类型参数.

                A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

                template <class T> class Y { /* ... */  }; 
                void func() {   
                      struct S { /* ... */ }; //local class   
                      Y< S > y1; // error: local type used as template-argument  
                      Y< S* > y2; // error: pointer to local type used as template-argument }
                

                来源和更多详细信息:http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=420

                总而言之,这个限制是一个错误,如果标准发展得更快的话,这个错误就会被修复得更快......

                To sum up, the restriction was a mistake that would have been fixed sooner if the standard was evolving faster...

                也就是说,今天大多数最新版本的通用编译器确实允许这样做,并提供 lambda 表达式.

                That said today most last versions of common compilers does allow it, along with providing lambda expressions.

                这篇关于使用带有 STL 算法的本地类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is the past-the-end iterator in STL C++?(STL C++ 中的最后迭代器是什么?)
                vector::at vs. vector::operator[](vector::at 与 vector::operator[])
                C++ equivalent of StringBuffer/StringBuilder?(C++ 等效于 StringBuffer/StringBuilder?)
                Adding types to the std namespace(将类型添加到 std 命名空间)
                Is the C++ std::set thread-safe?(C++ std::set 线程安全吗?)
                How to use std::find/std::find_if with a vector of custom class objects?(如何将 std::find/std::find_if 与自定义类对象的向量一起使用?)

                • <bdo id='UkFfA'></bdo><ul id='UkFfA'></ul>
                        <tbody id='UkFfA'></tbody>

                        <i id='UkFfA'><tr id='UkFfA'><dt id='UkFfA'><q id='UkFfA'><span id='UkFfA'><b id='UkFfA'><form id='UkFfA'><ins id='UkFfA'></ins><ul id='UkFfA'></ul><sub id='UkFfA'></sub></form><legend id='UkFfA'></legend><bdo id='UkFfA'><pre id='UkFfA'><center id='UkFfA'></center></pre></bdo></b><th id='UkFfA'></th></span></q></dt></tr></i><div id='UkFfA'><tfoot id='UkFfA'></tfoot><dl id='UkFfA'><fieldset id='UkFfA'></fieldset></dl></div>
                        <legend id='UkFfA'><style id='UkFfA'><dir id='UkFfA'><q id='UkFfA'></q></dir></style></legend>
                        <tfoot id='UkFfA'></tfoot>

                        <small id='UkFfA'></small><noframes id='UkFfA'>