Gab*_*due 2 alias templates using c++11 template-aliases
我无法使用我认为应该工作的使用声明:
#include <iostream>
#include <vector>
using namespace std;
template<typename C, typename V>
vector<typename C::iterator> find_all1(C& c, V v)
{
vector<typename C::iterator> res;
for (auto p = c.begin(); p!=c.end(); ++p) {
if (*p == v)
res.push_back(p);
}
return res;
}
template<typename T>
using Iterator<T> = typename T::iterator;
template <typename C, typename V>
vector<Iterator<C>> find_all2(C& c, V v)
{
vector<Iterator<C>> res;
for (auto p = c.begin(); p != c.end(); ++p) {
if (*p == v)
res.push_back(p);
}
return res;
}
int main(int argc, const char *argv[])
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这基本上是Stroustrup的书中的一个例子,它不会编译.第一部分(find_all1)可以,但尝试使用using声明的模板版本不会.
错误喷出的前导部分如下所示:
$ make
c++ -g -fPIC -std=c++0x -stdlib=libc++ -c -o iter_tests2.o iter_tests2.cpp
iter_tests2.cpp:18:7: error: no template named 'Iterator'; did you mean 'iterator'?
using Iterator<T> = typename T::iterator;
^~~~~~~~
iterator
/usr/bin/../lib/c++/v1/iterator:415:25: note: 'iterator' declared here
struct _LIBCPP_TYPE_VIS iterator
^
iter_tests2.cpp:18:15: error: partial specialization of alias templates is not permitted
using Iterator<T> = typename T::iterator;
^~~
Run Code Online (Sandbox Code Playgroud)
还有更多,但我认为重要的部分是关于使用声明.
您的语法不正确,请删除<T>:
template<typename T>
using Iterator = typename T::iterator;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
836 次 |
| 最近记录: |