如何使用模板在C++中使用“使用”(制作参数化别名)制作别名
How do I use Templates to make aliases with `using` (making parameterized aliases) in C++?
我目前正在阅读Bjarne Stroustrup的"C++编程语言"第4版。在本书的第一部分,我发现using
的用法如下所示:
// Error is in these 2 lines
template<typename T>
using Iterator<T> = typename T::iterator;
*有关完整的程序和错误消息,请参见 [**] *
这正是我在第 105 页中找到的。当我把它变成一个完整的程序并试图编译它时,g++
给了我这个错误按摩:
> g++ -std=c++14 -o fnd find_all.cpp
find_all.cpp:13:15: error: expected '=' before '<' token
using Iterator<T> = typename T::iterator;
^
find_all.cpp:13:15: error: expected type-specifier before '<' token
我在这段代码中找不到任何问题,(我是C++新手,我用我的小知识找不到问题)(更令人困惑的是我在 Bjarne 的书中找到了这一点)
有人可以告诉我为什么该代码会出错吗?
注意:但是,如果我用
typename C::iterator
替换Iterator<C>
(见下文),它工作正常,没有错误!
[**]完整的程序和错误消息:
// Error is in these 2 lines
template<typename T>
using Iterator<T> = typename T::iterator;
// -------------------------------------------
// For the completeness I'll include my complete program here
template<typename C, typename V>
vector<Iterator<C>> find_all(C& c, V v) // find all occurrences of v in c
{
vector<Iterator<C>> res;
for (auto p = c.begin(); p!=c.end(); ++p)
if (∗p==v)
res.push_back(p);
return res;
}
void test()
{
string m {"Mary had a little lamb"};
for (auto p : find_all(m, 'a'))
if (*p == 'a')
cerr << "string bug!n";
list<double> ld { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 1.1, 1.1 };
for (auto p : find_all(ld, 1.1))
if (*p == 1.1)
cerr << "list bug!n";
vector<string> strv { "blue", "yellow", "red", "white", "orange", "blue" };
for (auto p : find_all(strv, "blue"))
if (*p == "blue")
cerr << "string vector bug!n";
}
int main(void)
{
test();
return 0;
}
错误信息:
> g++ -std=c++14 -o fnd find_all.cpp
find_all.cpp:13:15: error: expected '=' before '<' token
using Iterator<T> = typename T::iterator;
^
find_all.cpp:13:15: error: expected type-specifier before '<' token
find_all.cpp:16:8: error: 'Iterator' was not declared in this scope
vector<Iterator<C>> find_all(C& c, V v)
^
find_all.cpp:16:17: error: template argument 1 is invalid
vector<Iterator<C>> find_all(C& c, V v)
^
find_all.cpp:16:17: error: template argument 2 is invalid
find_all.cpp:16:18: error: expected unqualified-id before '>' token
vector<Iterator<C>> find_all(C& c, V v)
^
find_all.cpp: In function 'void test()':
find_all.cpp:30:31: error: 'find_all' was not declared in this scope
for (auto p : find_all(m, 'a'))
^
find_all.cpp:35:32: error: 'find_all' was not declared in this scope
for (auto p : find_all(ld, 1.1))
^
find_all.cpp:40:37: error: 'find_all' was not declared in this scope
for (auto p : find_all(strv, "blue"))
必须省略第一个<T>
template<typename T>
using Iterator = typename T::iterator;
定义类模板或函数模板时,使用:
template <typename T> struct Foo { };
template <typename T> T bar() { return T{}; }
定义模板时不使用Foo<T>
或bar<T>
。
同样,使用模板定义别名时,需要使用:
template <typename T>
using Iterator = typename T::iterator;
^^ Don't include <T>
相关文章:
- 部分定义/别名模板模板参数
- 告诉c++编译器该参数没有别名
- 为什么我们不能重复使用具有不同模板参数的别名模板标识符?
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 模板模板参数和模板别名:编译器错误?
- 为模板参数包添加别名
- C++使用默认模板参数键入别名和转发声明
- 缺少别名模板C++参数列表
- 使用外部定义的模板类型作为模板参数的更通用模板的模板别名
- 必须非常量别名参数及其默认参数常量
- 作为模板参数 c++ 给出的类的别名模板
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- 参数化类的别名(或类型定义)内部类
- C++别名的模板参数包扩展
- 如何简化模板模板参数中的enable_if别名
- 包含可变参数包的第一个可转换类型的别名的结构
- C++ 11 个模板,参数包的别名
- 为什么模板引用类型不能用作模板类型别名参数?
- 具有模板类默认参数的 C++17 别名模板
- 模板别名、变量模板和自动类型推断无法推断模板参数