我试图让模板别名在 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
}
关于c++ - 模板别名不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7704391/