繁体   English   中英

模板别名不起作用

[英]Template aliases don't work

我正在尝试使用模板别名来处理clang,但它不起作用,尽管参考表说它确实如此

~~~~>$ 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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM