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

    <tfoot id='VMk0e'></tfoot>

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

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

      std::get 使用枚举类作为模板参数

      时间:2023-12-03
          <bdo id='N309t'></bdo><ul id='N309t'></ul>

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

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

                <tbody id='N309t'></tbody>

              • 本文介绍了std::get 使用枚举类作为模板参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 std::tuple 并定义了一个类枚举以某种方式命名"元组的每个字段,而忘记了它们的实际索引.

                I'm using a std::tuple and defined a class enum to somehow "naming" each of the tuple's fields, forgetting about their actual indexes.

                所以不要这样做:

                std::tuple<A,B> tup;
                /* ... */
                std::get<0>(tup) = bleh; // was it 0, or 1?
                

                我是这样做的:

                enum class Something {
                     MY_INDEX_NAME = 0,
                     OTHER_INDEX_NAME
                };
                
                std::tuple<A,B> tup;
                /* ... */
                std::get<Something::MY_INDEX_NAME> = 0; // I don't mind the actual index...
                

                问题是,由于这是使用 gcc 4.5.2 编译的,我现在已经安装了 4.6.1 版本,但我的项目无法编译.此代码段重现了错误:

                The problem is that, as this compiled using gcc 4.5.2, I've now installed the 4.6.1 version, and my project failed to compile. This snippet reproduces the error:

                #include <tuple>
                #include <iostream>
                
                enum class Bad {
                    BAD = 0
                };
                
                enum Good {
                    GOOD = 0
                };
                
                int main() {
                    std::tuple<int, int> tup(1, 10);
                    std::cout << std::get<0>(tup) << std::endl;
                    std::cout << std::get<GOOD>(tup) << std::endl; // It's OK
                    std::cout << std::get<Bad::BAD>(tup) << std::endl; // NOT!
                }
                

                该错误基本上是说没有与我对 std::get 的调用相匹配的重载:

                The error basically says that there's no overload that matches my call to std::get:

                test.cpp: In function ‘int main()’:
                test.cpp:16:40: error: no matching function for call to ‘get(std::tuple<int, int>&)’
                test.cpp:16:40: note: candidates are:
                /usr/include/c++/4.6/utility:133:5: note: template<unsigned int _Int, class _Tp1, class _Tp2> typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(std::pair<_Tp1, _Tp2>&)
                /usr/include/c++/4.6/utility:138:5: note: template<unsigned int _Int, class _Tp1, class _Tp2> const typename std::tuple_element<_Int, std::pair<_Tp1, _Tp2> >::type& std::get(const std::pair<_Tp1, _Tp2>&)
                /usr/include/c++/4.6/tuple:531:5: note: template<unsigned int __i, class ... _Elements> typename std::__add_ref<typename std::tuple_element<__i, std::tuple<_Elements ...> >::type>::type std::get(std::tuple<_Elements ...>&)
                /usr/include/c++/4.6/tuple:538:5: note: template<unsigned int __i, class ... _Elements> typename std::__add_c_ref<typename std::tuple_element<__i, std::tuple<_Elements ...> >::type>::type std::get(const std::tuple<_Elements ...>&)
                

                那么,有什么方法可以将我的枚举类用作 std::get 的模板参数?这是不是编译的东西,并在 gcc 4.6 中修复了?我可以使用简单的枚举,但我喜欢枚举类的作用域属性,所以如果可能的话,我更喜欢使用后者.

                So, is there any way I can use my enum class as a template argument to std::get? Was this something that wasn't ment to compile, and was fixed in gcc 4.6? I could use a simple enum, but I like the scoping properties of enum classes, so I'd prefer to use the latter if it's possible.

                推荐答案

                C++11 引入的强类型枚举不能隐式转换为 int 类型的整数值,而 std::get 期望模板参数是整型.

                The strongly-typed enums introduced by C++11 cannot be implicitly converted into integral values of type say int, while std::get expects the template argument to be integral type.

                您必须使用 static_cast 来转换枚举值:

                You've to use static_cast to convert the enum values:

                std::cout <<std::get<static_cast<int>(Bad::BAD)>(tup)<< std::endl; //Ok now!
                

                或者您可以选择转换为底层整数类型为:

                Or you can choose to convert into underlying integral type as:

                //note that it is constexpr function
                template <typename T>
                constexpr typename std::underlying_type<T>::type integral(T value) 
                {
                    return static_cast<typename std::underlying_type<T>::type>(value);
                }
                

                然后将其用作:

                std::cout <<std::get<integral(Bad::BAD)>(tup)<< std::endl; //Ok now!
                

                这篇关于std::get 使用枚举类作为模板参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何从 C++ rand() 获取当前种子? 下一篇:通过 std::ifstream 从字符串中获取每个值

                相关文章

              • <tfoot id='RLbSm'></tfoot>

              • <small id='RLbSm'></small><noframes id='RLbSm'>

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