barney
barney

Reputation: 2202

Templates :: Stroustrup's example is not compiling

"No matching member function push_back"

In Bjarne's original version vector of C was written like

vector<Value_type<C>*> res;

but this Value_type template is not working as well so I just replaced it with C* pointer, still doesn't help

#include <vector>
#include <string>
#include <iostream>

using namespace std;

template <typename C, typename V>
vector<C*> find_all(C& c, V v) {
    vector<C*> res;
    for (auto &x : c) {
        if (x==v)
            res.push_back(&x);
    }
    return res;
}

int main() {
    string s = "werqewreqwreqw";
    for (const auto p : find_all(s,'q')) {
        cout << p;
    }

}

Upvotes: 0

Views: 94

Answers (1)

user5365879
user5365879

Reputation: 26

C* is not the same as C::value_type*. Actually, there's already a typedef that represents the same concept, which is C::pointer.

template <typename C, typename V>
vector<typename C::pointer> find_all(C& c, V v) {
    vector<typename C::pointer> res;

FYI, C::pointer will be std::allocator_traits<T>::pointer, which is furthermore just T* for the default allocator or value_type*. I'm not entirely sure why Value_type was used but I'm assuming it was Bjarne's idea for a meta function that replaces typename C::value_type.

Upvotes: 1

Related Questions