我发现,GCC 5.1
允许像list<auto>
这样的类型作为函数和lambda参数类型。虽然这个功能非常有趣甚至有用,但我无法找到解释此行为的任何扩展或标准。
关于此功能,GCC
方面是否有任何解释?
我做了一个简易型开关作为例子:
#include <iostream>
#include <vector>
#include <typeinfo>
#include <tuple>
using namespace std;
void matchType(const tuple<auto,auto,auto>& x){
cout << "its a tuple<auto,auto,auto>" << endl;
}
void matchType(const tuple<auto,auto>& x){
cout << "its a tuple<auto,auto>" << endl;
}
void matchType(const vector<auto>& x){
cout << "its a vector<" << typeid(typename decay_t<decltype(x)>::value_type).name() << ">" << endl;
}
void matchType(const auto& x){
cout << "its a " << typeid(x).name() << endl;
}
int main(){
matchType(make_tuple(23,55,11));
matchType(make_tuple("hey","it works"));
matchType(vector<string>{});
matchType(vector<int>{});
matchType("test");
}
输出:
its a tuple<auto,auto,auto>
its a tuple<auto,auto>
its a vector<NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE>
its a vector<i>
its a A5_c