簡體   English   中英

如何使用模板在C ++中使用`using`(制作參數化別名)創建別名?

[英]How do I use Templates to make aliases with `using` (making parameterized aliases) in C++?

我正在閱讀Bjarne Stroustrup的“The C ++ Programming Language”第4版。 在本書的第一部分,我發現的使用using看起來像如下:

// Error is in these 2 lines
template<typename T>
using Iterator<T> = typename T::iterator;

*有關完整程序和錯誤消息,請參閱[**] *

這正是我在頁面105中找到的。當我把它變成一個完整的程序並試圖編譯它時, g++給了我這個錯誤masseage:

> 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM