我使用std::tuple
, 并定义了一种类 enum, 以某种方式“命名”每个 tupple 字段, 忘记了它们的实际索引 。
因此,与其这样做:
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 版本,我的工程没有编译。
#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
的调用 :
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中固定的?我可以使用一个简单的昆虫,但是我喜欢昆虫类的范围属性,所以如果可能,我宁愿使用后者。