c++ - 模板别名不起作用

标签 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.

我做错了吗?

最佳答案

您的第二个命令(带有 -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/

相关文章:

c++ - 将指针转换为 uint64_t

c++ - 使用 atlbase.h 是否会使我编译的应用程序具有一些额外的依赖性?

c++ - C++线程连接会立即返回吗?

c++ - std::array 类成员在编译时设置?

clang 以错误的方式优化了代码

c++ - Clang-tidy 启用 C++17 扩展

c++ - std::vector 比 std::map 更快的键查找?

c++ - 多对象绘图 (OpenGL)

c++ - 如何处理异构容器中的类型转换

c++ - 'constexpr' 非静态成员函数在 C++1y 中不会隐式为 'const';添加 'const' 以避免行为改变