c – 模板别名不起作用
发布时间:2020-12-16 09:37:41 所属栏目:百科 来源:网络整理
导读:我正在尝试使用模板别名来处理clang,但它不起作用,尽管 reference sheet说它确实如此 ~~~~$cat template_alias.cpp #include vectorusing namespace std;templatetypename Tusing DoubleVec = vectorvectorT;int main() { return 0; }~~~~$clang template_al
我正在尝试使用模板别名来处理clang,但它不起作用,尽管
reference sheet说它确实如此
~~~~>$cat template_alias.cpp #include <vector> using namespace std; template<typename T> using DoubleVec = vector<vector<T>>; int main() { return 0; } ~~~~>$clang template_alias.cpp -o template_alias template_alias.cpp:6:19: warning: alias declarations accepted as a C++0x extension [-Wc++0x-extensions] using DoubleVec = vector<vector<T>>; ^ template_alias.cpp:6:34: error: a space is required between consecutive right angle brackets (use '> >') using DoubleVec = vector<vector<T>>; ^~ > > template_alias.cpp:6:1: error: cannot template a using declaration using DoubleVec = vector<vector<T>>; ^ 1 warning and 2 errors generated. ~~~~>$clang -std=c++0x template_alias.cpp -o template_alias template_alias.cpp:6:1: error: cannot template a using declaration using DoubleVec = vector<vector<T>>; ^ 1 error generated. 我做错了吗? 解决方法
你的第二个命令(-std = c 0x)是正确的,你的测试用例也是如此.您可能在支持模板别名之前使用了clang版本.您可以通过执行以下操作来检查:
#if __has_feature(cxx_alias_templates) 以下是clang使用的功能测试宏的完整列表: http://clang.llvm.org/docs/LanguageExtensions.html#checking_upcoming_features 这是处理模板别名支持之间的过渡期的一种有点不愉快的方法,而不是: #include <vector> using namespace std; #if __has_feature(cxx_alias_templates) template<typename T> using DoubleVec = vector<vector<T>>; #else template<typename T> struct DoubleVec { typedef vector<vector<T> > type; }; #endif int main() { #if __has_feature(cxx_alias_templates) DoubleVec<int> v; #else DoubleVec<int>::type v; #endif } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |