从未调用重载的函数模板

fti*_*sem 4 c++ operator-overloading template-specialization

我喜欢模板,至少我会理解他们;-).我使用模板实现了重载运算符.我现在正在尝试专门调用函数.

这是我做的:

class Terminallog {
public:

    Terminallog();
    Terminallog(int);
    virtual ~Terminallog();

    template <class T>
    Terminallog & operator<<(const T &v);
    template <class T>
    Terminallog & operator<<(const std::vector<T> &v);
    template <class T>
    Terminallog & operator<<(const std::vector<T> *v);
    template <class T, size_t n>
    Terminallog & operator<<(const T(&v)[n]);
    Terminallog & operator<<(std::ostream&(*f)(std::ostream&));
    Terminallog & operator<<(const char v[]);

    //stripped code

};

//stripped code

template <class T>
Terminallog &Terminallog::operator<<(const T &v) {
    if (this->lineendet == true) {
        this->indent();
    }
    this->lineendet = false;
    std::cout << v;
    return *this;
}

template <class T>
Terminallog &Terminallog::operator<<(const std::vector<T> &v) {
    for (unsigned int i = 0; i < v.size(); i++) {
        std::cout << std::endl;
        this->indent();
        std::cout << i << ": " << v.at(i);
    }
    std::cout << std::flush;
    return *this;
}

template <class T>
Terminallog &Terminallog::operator<<(const std::vector<T> *v) {
    for (unsigned int i = 0; i < v->size(); i++) {
        std::cout << std::endl;
        this->indent();
        std::cout << i << ": " << v->at(i);
    }
    std::cout << std::flush;
    return *this;
}

template <class T, size_t n>
Terminallog &Terminallog::operator<<(const T(&v)[n]) {
    unsigned int elements = sizeof (v) / sizeof (v[0]);
    for (unsigned int i = 0; i < elements; i++) {
        std::cout << std::endl;
        this->indent();
        std::cout << i << ": " << v[i];
    }
    std::cout << std::flush;
    return *this;
}

inline
Terminallog &Terminallog::operator<<(std::ostream&(*f)(std::ostream&)) {
    if (f == static_cast<std::ostream & (*)(std::ostream&)> (std::endl)) {
        this->lineendet = true;
    }
    std::cout << f;
    return *this;
}

inline
Terminallog &Terminallog::operator<<(const char v[]) {
    if (this->lineendet == true) {
        std::cout << std::endl;
        this->indent();
        std::cout << v;
    }
    this->lineendet = false;
    std::cout << v;
    return *this;
}

//sripped code
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试类似的东西

vector<int> *test3 = new vector<int>;
    test3->push_back(1);
    test3->push_back(2);
    test3->push_back(3);
    test3->push_back(4);

Terminallog clog(3);
clog << test3;
Run Code Online (Sandbox Code Playgroud)

编译得很好.但是,执行代码时,它会打印test3的地址,而不是所有元素.我得出结论,编译器认为,那

Terminallog & operator<<(const T &v);
Run Code Online (Sandbox Code Playgroud)

是一个更好的匹配.但是我不知道该怎么办.我的代码中的错误在哪里?为什么是

Terminallog & operator<<(const std::vector<T> *v);
Run Code Online (Sandbox Code Playgroud)

从未打电话?

Cub*_*bbi 5

test3您的代码中的类型是std::vector<int> *,但没有Terminallog::operator<<采用该类型的参数.有一个需要const std::vector<int> *,如果你这样做就会被调用

clog << const_cast<const vector<int>*>(test3);
Run Code Online (Sandbox Code Playgroud)

顺便说一句,新的矢量几乎不是一个好主意.