模板别名不起作用

时间:2011-10-09 15:02:19

标签: c++ c++11 clang template-aliases

我正在尝试使用模板别名来处理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.

我做错了吗?

1 个答案:

答案 0 :(得分:7)

你的第二个命令(-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
}
相关问题