• <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 和作用域运算符

        时间:2023-10-17
            <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 和作用域运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么这个 C++ 工作?(同名变量) 下一篇:基于作用域的锁守卫和返回值的时间安排

                  相关文章

                  <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>
                    1. <tfoot id='DGq6P'></tfoot>