Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm currently reading Bjarne Stroustrup's "The C++ Programming Language" 4th Edition. In the first parts of the book, I found an usage of using looks like following:

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

*see [**] for complete program and error message*

This is exactly what I found in the page 105. When I turned this into a complete program and tried to compile it, g++ gave me this error 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

I can't find any problem in this code, (I'm new to C++, I can't find the problem with my little knowledge)( More confusingly I found this on Bjarne's book )

Could someone tell me why does that code makes an error?

NOTE: However If I replaced Iterator<C> with typename C::iterator (see below), It works fine, there is no error!

[**]Complete Program and Error Message:

// 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;
}

ERROR MESSAGE:

> 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"))
like image 790
0xEDD1E Avatar asked Mar 13 '16 06:03

0xEDD1E


People also ask

What is an alias in CPP?

You can use an alias declaration to declare a name to use as a synonym for a previously declared type. (This mechanism is also referred to informally as a type alias). You can also use this mechanism to create an alias template, which can be useful for custom allocators.

What is an alias type?

A type alias is a shorter name for a type. For example, you could create a User alias like this: type alias User = { name : String , age : Int } Rather than writing the whole record type all the time, we can just say User instead.

What are parameterized templates?

Enterprise Architect supports template or parameterized Classes, which specify parameters that must be defined by any binding Class. Parameterized Classes are commonly implemented in C++; Enterprise Architect imports and generates templated Classes for C++.

Can you make templates in C?

Static templates used in C are similar to the templates from the C++ language, because they depend on the actual type members, such as in the case of a struct . In C, the only native means of creating static templates are through the use of macros. printf("%d", foo);


1 Answers

First <T> must be omitted

template<typename T>
using Iterator = typename T::iterator;
like image 122
user2807083 Avatar answered Sep 27 '22 19:09

user2807083