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

      1. <small id='Ask1i'></small><noframes id='Ask1i'>

        <legend id='Ask1i'><style id='Ask1i'><dir id='Ask1i'><q id='Ask1i'></q></dir></style></legend>
          <bdo id='Ask1i'></bdo><ul id='Ask1i'></ul>

        <tfoot id='Ask1i'></tfoot>

        C++ 中的 decltype 和作用域运算符

        decltype and the scope operator in C++(C++ 中的 decltype 和作用域运算符)
            <i id='jua92'><tr id='jua92'><dt id='jua92'><q id='jua92'><span id='jua92'><b id='jua92'><form id='jua92'><ins id='jua92'></ins><ul id='jua92'></ul><sub id='jua92'></sub></form><legend id='jua92'></legend><bdo id='jua92'><pre id='jua92'><center id='jua92'></center></pre></bdo></b><th id='jua92'></th></span></q></dt></tr></i><div id='jua92'><tfoot id='jua92'></tfoot><dl id='jua92'><fieldset id='jua92'></fieldset></dl></div>
            <tfoot id='jua92'></tfoot>
              <tbody id='jua92'></tbody>

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

            1. <legend id='jua92'><style id='jua92'><dir id='jua92'><q id='jua92'></q></dir></style></legend>

              • <bdo id='jua92'></bdo><ul id='jua92'></ul>
                • 本文介绍了C++ 中的 decltype 和作用域运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我需要获取实例化模板时提供的类型.考虑以下示例:

                  I need to obtain the type which was supplied when instantiating a template. Consider the following example:

                  template <typename T> struct Foo
                  {
                    typedef T TUnderlying;
                  };
                  
                  static Foo<int> FooInt;
                  
                  class Bar
                  {
                  public:
                    auto Automatic() -> decltype(FooInt)::TUnderlying
                    {
                      return decltype(FooInt)::TUnderlying();
                    }
                  };
                  
                  int main()
                  {
                    Bar bar;
                    auto v = bar.Automatic();
                      return 0;
                  }
                  

                  此代码的问题是将作用域运算符与 decltype 一起使用.Visual C++ 2010 抱怨如下:

                  Problem with this code is using the scope operator together with decltype. Visual C++ 2010 complains like this:

                  错误 C2039:TUnderlying":不是全局命名空间"的成员

                  error C2039: 'TUnderlying' : is not a member of '`global namespace''

                  我在维基百科上收集了一些关于该主题的信息:

                  I gathered some information on the topic on Wikipedia:

                  在评论 C++0x 的正式委员会草案时,日本 ISO 成员团体指出范围运算符 (::) 不能应用于 decltype,但它应该适用.这在以下情况下很有用从实例中获取成员类型(嵌套类型),如下所示":[16]

                  While commenting on the formal Committee Draft for C++0x, the Japanese ISO member body noted that "a scope operator(::) cannot be applied to decltype, but it should be. It would be useful in the case to obtain member type(nested-type) from an instance as follows":[16]

                  vector<int> v;
                  decltype(v)::value_type i = 0; // int i = 0;
                  

                  David Vandevoorde 解决了这个问题和类似问题,并于 2010 年 3 月投票通过了工作文件.

                  This, and similar issues were addressed by David Vandevoorde, and voted into the working paper in March 2010.

                  所以我认为 Visual C++ 2010 没有实现这个.我想出了这个解决方法:

                  So I reckon the Visual C++ 2010 does not have this implemented. I came up with this workaround:

                  template <typename T> struct ScopeOperatorWorkaroundWrapper
                  {
                    typedef typename T::TUnderlying TTypedeffedUnderlying;
                  };
                  
                  auto Automatic() -> ScopeOperatorWorkaroundWrapper<decltype(FooInt)>::TTypedeffedUnderlying
                  {
                    return ScopeOperatorWorkaroundWrapper<decltype(FooInt)>::TTypedeffedUnderlying();
                  }
                  

                  我是否错过了任何更优雅、更简洁的解决方案?

                  Did I miss any solution which is more elegant and less verbose?

                  推荐答案

                  这透明地用基于模板的解决方法替换了 decltype 关键字.一旦您不再需要支持 MSVC2010,您就可以在不更改任何用户代码的情况下删除宏定义:

                  This transparently replaces the decltype keyword with the template based workaround. Once you no longer need to support MSVC2010 you can remove the macro definition without changing any user code:

                  #if _MSC_VER == 1600
                  #include <utility>
                  #define decltype(...) 
                    std::identity<decltype(__VA_ARGS__)>::type
                  #endif
                  

                  这允许它在 MSVC10 上编译和工作:

                  Which allows this to compile and work on MSVC10:

                  std::vector<int> v;
                  decltype(v)::value_type i = 0;
                  

                  <小时>

                  请注意,std::identity 不是 C++ 标准的一部分,但在这里依赖它是安全的,因为解决方法仅限于包含 std::identity 的编译器 在其标准库实现中.


                  Note that std::identity isn't part of the C++ standard, but it's safe to rely on it here as the workaround is limited to a compiler which includes std::identity in its standard library implementation.

                  这篇关于C++ 中的 decltype 和作用域运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Is Type(::x); valid?(是类型(::x);有效的?)
                  Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                  Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                  Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                  No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                  Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                  <i id='DGq6P'><tr id='DGq6P'><dt id='DGq6P'><q id='DGq6P'><span id='DGq6P'><b id='DGq6P'><form id='DGq6P'><ins id='DGq6P'></ins><ul id='DGq6P'></ul><sub id='DGq6P'></sub></form><legend id='DGq6P'></legend><bdo id='DGq6P'><pre id='DGq6P'><center id='DGq6P'></center></pre></bdo></b><th id='DGq6P'></th></span></q></dt></tr></i><div id='DGq6P'><tfoot id='DGq6P'></tfoot><dl id='DGq6P'><fieldset id='DGq6P'></fieldset></dl></div>
                  <legend id='DGq6P'><style id='DGq6P'><dir id='DGq6P'><q id='DGq6P'></q></dir></style></legend>

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

                          <bdo id='DGq6P'></bdo><ul id='DGq6P'></ul>
                        • <tfoot id='DGq6P'></tfoot>

                            <tbody id='DGq6P'></tbody>